Container multi-threaded access to resources

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

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

 

Semaphore <used to avoid blocking caused by thread preemption>

If there are three pick-up windows to pick up meals and five people come to eat, then two people must be in a waiting state. Only those who know that the first three people have left the follow-up can pick up the meal. // code example

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 ( "Permits remaining = " + semaphore .availablePermits());
                    Thread.sleep(1000);
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
            }
        });
    }
}

A round-robin fence allows a group of threads to wait on each other until some common barrier point is reached.

/**
 * 循环栅栏 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 ( "--> Satisfy the condition <-- " + 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 
                ( ) + "Continue to execute" );
            } catch (InterruptedException e) {
                e.printStackTrace ();
            } catch (BrokenBarrierException e) {
                e.printStackTrace ();
            }
        }
    }
}
Thread- 3 waits for CyclicBarrier
Thread- 2 waits for CyclicBarrier
Thread- 1 waits for CyclicBarrier
Thread- 4 wait for CyclicBarrier
Thread- 5 waiting for CyclicBarrier
--> Satisfy the condition <-- 5
 Thread- 4 continue to execute
Thread- 2 continues execution
Thread- 1 continues execution
Thread- 3 continues execution
Thread- 5 continues execution

latch <allows one or more threads to wait until a condition is met>

/**
 * 闭锁
 */
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 ( " Main thread waiting" );
        latch.await();
        System.out.println ( " Main thread continues to execute" );
    }

    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 ();
            }
        }
    }
}
main thread waiting
Thread- 4 performs the operation
Thread- 1 performs the operation
Thread- 3 performs the operation
Thread- 5 performs the operation
Thread- 2 performs the operation
The main thread continues to execute

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325461566&siteId=291194637