容器 多线程访问资源

1、ArrayBlockingQueue https://blog.csdn.net/qq_23359777/article/details/70146778

2、CopyOnWriteArrayList https://blog.csdn.net/hua631150873/article/details/51306021

信号量 <用于避免线程抢占资源造成的阻塞>

假如有有三个取餐窗口可以取餐,有五个人来吃饭,那么有两个人就必须处于等待状态。知道前三个人有离开后续的人才能进行取餐。       // 代码实例

public static void main() {
    ExecutorService service = Executors.newFixedThreadPool(3);
    final Semaphore semaphore = new Semaphore(3);
    for (int i = 0; i < 5; i++) {
        service.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    semaphore.acquire();
                    System.out.println("剩余许可 = " + semaphore.availablePermits());
                    Thread.sleep(1000);
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

循环栅栏 允许一组线程互相等待,知道达到某个公共屏障点。

/**
 * 循环栅栏 zhalan
 */
public static class CyclicBarrierTest {
    private static final int SIZE = 5;
    private static CyclicBarrier cyclicBarrier;

    public static void main(String[] args) {
        cyclicBarrier = new CyclicBarrier(SIZE, new Runnable() {
            @Override
            public void run() {
                System.out.println("--> 满足条件 <-- " + cyclicBarrier.getParties());
            }
        });
        for (int i = 0; i < 5; i++) {
            new Work().start();
        }
    }

    static class  Work extends Thread{
        @Override
        public void run() {
            super.run();
            System.out.println(Thread.currentThread().getName() + "等待 CyclicBarrier");
            try {
                cyclicBarrier.await();
                System.out.println(Thread.currentThread().getName() + "继续执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }
}
Thread-3 等待 CyclicBarrier
Thread-2 等待 CyclicBarrier
Thread-1 等待 CyclicBarrier
Thread-4 等待 CyclicBarrier
Thread-5 等待 CyclicBarrier
--> 满足条件 <-- 5
Thread-4 继续执行
Thread-2 继续执行
Thread-1 继续执行
Thread-3 继续执行
Thread-5 继续执行

闭锁  <允许一个或多个线程一直等待,直到满足条件>

/**
 * 闭锁
 */
public static class CountDownLatchTest {
    private static final int SIZE = 5;

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(SIZE);
        for (int i = 0; i < 5; i++) {
            new Work(latch).start();
        }
        System.out.println("主线程等待");
        latch.await();
        System.out.println("主线程继续执行");
    }

    static class  Work extends Thread{
        CountDownLatch latch;
        Work( CountDownLatch latch){
            this.latch = latch;
        }
        @Override
        public void run() {
            super.run();
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + " 执行操作");
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
主线程等待
Thread-4 执行操作
Thread-1 执行操作
Thread-3 执行操作
Thread-5 执行操作
Thread-2 执行操作
主线程继续执行

猜你喜欢

转载自my.oschina.net/u/3342652/blog/1785049