容器 多线程访问资源

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

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

 

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

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

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();
                }
            }
        });
    }
}

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

/**
 * 循环栅栏 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 执行操做
主线程继续执行
相关文章
相关标签/搜索