java Thread深入了解(四)

概念介绍

悲观锁是指假设并发更新冲突会发生,所以不管冲突是否真的发生,都会使用锁机制。
相对悲观锁而言,乐观锁机制采取了更加宽松的加锁机制

秒杀案例

下面是模拟秒杀的环境 每个返回操作前必须休眠100ms模拟数据库和其他的耗时操作

/**
 * 秒杀服务
 * Created by 51422 on 2017/9/6.
 */
public class MiaoSha {

    public static int goods=10;

    /**
     * 返回订单号
     * @return -1 表示无货 否则货号
     * @throws InterruptedException
     */
    public int getGoods() throws InterruptedException {
        //todo 在这里完成获取货物代码
        Thread.sleep(100);//处理延时100ms
        return -1;
    }
}


/**
 * 测试案例
 */
public void testMiaoSha(){
    final MiaoSha miaoSha=new MiaoSha();
    long st=System.currentTimeMillis();
    ScheduledExecutorService service = Executors.newScheduledThreadPool(500);
    for(int i=0;i<50000;i++){
        service.execute(new Runnable() {
            public void run() {
                try {
                    int good=miaoSha.getGoods();
                    if(good>0){
                        System.out.println("获取到订单"+good);
                    };
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    service.shutdown();
    try {
        service.awaitTermination(2, TimeUnit.DAYS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("耗时"+(System.currentTimeMillis()-st)+"ms");
}

悲观锁处理

/**
 * 秒杀服务
 * Created by 51422 on 2017/9/6.
 */
public class MiaoSha {

    public static int goods=10;

    /**
     * 返回订单号
     * @return -1 表示无货 否则货号
     * @throws InterruptedException
     */
    public int getGoods() throws InterruptedException {
        //todo 在这里完成获取货物代码
        synchronized(this){
            if(goods>0){
                int no=goods;
                goods--;
                Thread.sleep(100);//处理延时100ms
                return no;
            }
            Thread.sleep(100);//处理延时100ms
            return -1;
        }
    }
}

乐观锁处理

/**
 * 秒杀服务
 * Created by 51422 on 2017/9/6.
 */
public class MiaoSha {

    public static int goods=10;
    public static int version=0;

    /**
     * 返回订单号
     * @return -1 表示无货 否则货号
     * @throws InterruptedException
     */
    public int getGoods() throws InterruptedException {
        //todo 在这里完成获取货物代码
        int v=-1;
        if(goods>0&&(v=version)>0){
            if(v==version){
                synchronized(this){
                    if(v==version){
                        version++;
                        int no=goods;
                        goods--;
                        Thread.sleep(100);//处理延时100ms
                        return no;
                    }
                }
            }
        }
        Thread.sleep(100);//处理延时100ms
        return -1;
    }
}

线程进入的时候获取version 同当前version对比 如果相同则参加锁竞争 竞争获得锁后再次对比version 防止version过期 如果version还是同当前版本号相同 则获取订单,这样会有效的同步线程,加快服务处理速度且保证货物不会被超发

信号量Semaphore

锁能保证同时只有一个线程使用资源 不能让指定个数的线程同时访问资源,这个时候就需要信号量,可以保证指定个数的线程访问资源,达到限流或者限速,比如同时只有2个客户端可以连接到服务器并使用。

public static void SamephoreTest(){
    Semaphore samephore=new Semaphore(2);
    for(int i=0;i<3;i++){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    samephore.acquire();
                    System.out.println("我访问了!");
                    Thread.sleep(5000);//休眠5s
                    samephore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

acquire获取许可 是个阻塞的方法
release释放许可
acquire和release必须成对出现 否则会产生资源死锁

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概念介绍 悲观锁是指假设并发更新冲突会发生,所以不管冲突是否真的发生,都会使用锁机制。相对悲观锁而言,乐观锁机制采...
    代码界的扫地僧阅读 981评论 0 1
  • 1乐观锁 和悲观锁 简绍: 悲观锁是指假设并发更新冲突会发生,所以不管冲突是否真的发生,都会使用锁机制。相对悲观锁...
    Charon_Pluto阅读 1,742评论 0 0
  • 下面是我自己收集整理的Java线程相关的面试题,可以用它来好好准备面试。 参考文档:-《Java核心技术 卷一》-...
    阿呆变Geek阅读 14,999评论 14 507
  • 线程1.优先级2.守护线程3.常用方法4.线程状态 多线程操作1.volatile2.Atomic3.CAS 锁[...
    Jude95阅读 12,079评论 4 121
  • 真正要离开的人都是悄无声息的。 小妹今年就要参加高考。她每隔几天就要给我打一个电话,噼里啪啦地向我诉说自己的学习近...
    步青城阅读 2,953评论 8 4