JAVA并发编程2

JAVA并发编程2

CountDownLatch

该锁用以希望所有子线程结束后,才开始继续往下走

代码

CountDownLatch countDownLatch = new CountDownLatch(6);
for (int i = 0; i < 6; i++) {
    new Thread(()->{
        try {
            Thread.sleep(15);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" go out");
        countDownLatch.countDown();
    }, i+"").start();
}
System.out.println("start to wait at " + System.currentTimeMillis());
countDownLatch.await(); // 在这里等待
System.out.println("end to wait at " + System.currentTimeMillis());

结果

start to wait at 1656515615320
1 go out
5 go out
2 go out
0 go out
3 go out
4 go out
end to wait at 1656515615335

CyclicBarrier

每个子线程都等待在同一个地方,直到barrier内的线程执行完成,子线程才继续执行

代码

CyclicBarrier barrier = new CyclicBarrier(7, () -> {
    System.out.println("success to save grandpa");
});

for (int i = 0; i < 7; i++) {
    new Thread(() -> {
        System.out.println(Thread.currentThread().getName() + " come to save");
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " end to save");
    }, i + "").start();
}

结果

0 come to save
3 come to save
1 come to save
2 come to save
5 come to save
4 come to save
6 come to save
success to save grandpa
6 end to save
0 end to save
3 end to save
5 end to save
2 end to save
1 end to save
4 end to save

Semaphore

semaphore允许多个线程同时几个线程同时访问临界资源

代码

Semaphore semaphore = new Semaphore(4);
for (int i = 0; i < 10; i++) {
    new Thread(() -> {
        try {
            semaphore.acquire();
            System.out.println(Thread.currentThread().getName() + " coming in at" + System.currentTimeMillis());
            Thread.sleep((new Random()).nextInt(200));
            System.out.println(Thread.currentThread().getName() + " coming out at" + System.currentTimeMillis());
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }, i + "").start();
}

结果

0 coming in at1656517299135
2 coming in at1656517299135
1 coming in at1656517299135
3 coming in at1656517299136
1 coming out at1656517299149
4 coming in at1656517299149
4 coming out at1656517299252
5 coming in at1656517299252
3 coming out at1656517299271
6 coming in at1656517299271
2 coming out at1656517299276
7 coming in at1656517299276
0 coming out at1656517299292
8 coming in at1656517299292
5 coming out at1656517299319
9 coming in at1656517299319
7 coming out at1656517299324
6 coming out at1656517299399
8 coming out at1656517299482
9 coming out at1656517299506

readwritelock

不加锁的情况

一个的写入不会等待另一个写入完成

代码
MyReadWrite myReadWrite = new MyReadWrite();
for (int i = 0; i < 5; i++) {
    new Thread(myReadWrite::write, String.valueOf(i)).start();
}

for (int i = 5; i < 10; i++) {
    new Thread(myReadWrite::read, String.valueOf(i)).start();
}
class MyReadWrite {
    public void write() {
        try {
            System.out.println(Thread.currentThread().getName() + "start to write at" + System.currentTimeMillis());
            Thread.sleep((new Random()).nextInt(300));
            System.out.println(Thread.currentThread().getName() + "finish to write at " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void read() {
        try {
            System.out.println(Thread.currentThread().getName() + "begin to read at " + System.currentTimeMillis());
            Thread.sleep((new Random()).nextInt(300));
            System.out.println(Thread.currentThread().getName() + "end to read at " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
结果
0start to write at1656518986758
4start to write at1656518986758
3start to write at1656518986758
1start to write at1656518986758
2start to write at1656518986758
5begin to read at 1656518986758
6begin to read at 1656518986758
7begin to read at 1656518986758
8begin to read at 1656518986758
9begin to read at 1656518986758
0finish to write at 1656518986760
8end to read at 1656518986783
9end to read at 1656518986801
1finish to write at 1656518986831
6end to read at 1656518986835
4finish to write at 1656518986845
3finish to write at 1656518986932
7end to read at 1656518986995
2finish to write at 1656518987009
5end to read at 1656518987031

枷锁的情况

加锁后,写之间不会被插入,读锁进入时,写锁不能进入,但是读锁的线程可以继续进入

代码
class MyReadWrite {
    ReadWriteLock lock = new ReentrantReadWriteLock();

    public void write() {
        try {
            Thread.sleep((new Random()).nextInt(300));
            lock.writeLock().lock();
            System.out.println(Thread.currentThread().getName() + "start to write at" + System.currentTimeMillis());
            Thread.sleep((new Random()).nextInt(300));
            System.out.println(Thread.currentThread().getName() + "finish to write at " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.writeLock().unlock();
        }
    }

    public void read() {
        try {
            Thread.sleep((new Random()).nextInt(300));
            lock.readLock().lock();
            System.out.println(Thread.currentThread().getName() + "begin to read at " + System.currentTimeMillis());
            Thread.sleep((new Random()).nextInt(300));
            System.out.println(Thread.currentThread().getName() + "end to read at " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.readLock().unlock();
        }
    }
}
结果
1start to write at1656519538091
1finish to write at 1656519538233
5begin to read at 1656519538233
5end to read at 1656519538525
3start to write at1656519538525
3finish to write at 1656519538546
0start to write at1656519538546
0finish to write at 1656519538578
2start to write at1656519538578
2finish to write at 1656519538738
8begin to read at 1656519538738
7begin to read at 1656519538738
9begin to read at 1656519538738
7end to read at 1656519538816
9end to read at 1656519538825
8end to read at 1656519539000
4start to write at1656519539000
4finish to write at 1656519539239
6begin to read at 1656519539239
6end to read at 1656519539406
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值