Multi-threaded task - loopback fence - thread counter - Semaphore

First, the loop fence -CyclicBarrier

1. Definitions
  • The number of initialization thread mark, when the number reaches the number of threads started marking threads, concurrently. All concurrent threads remain in the same state.
2, Scene
  • Suit the needs of each other when the thread waits for the other to complete, such as multi-threaded calculation, and summarizing the results.
3, Code

Run sequence: create a thread and start, main function is not blocked, cb.await (); After the block waiting for the thread to the number of full implementation of the method after adding satisfied, after the thread to continue.

CyclicBarrier cb=new CyclicBarrier(5,()->{
    System.out.println(Thread.currentThread()+"所有线程准备完毕,由最后一个线程执行此方法");
    System.out.println(Thread.currentThread()+"执行聚合操作");
});
IntStream.range(0,5).forEach(i->{
    new Thread(()->{
        System.out.println(Thread.currentThread()+"启动");
        try {
            System.out.println(Thread.currentThread()+"执行业务逻辑");
            cb.await();//一般之后没有代码
            System.out.println(Thread.currentThread()+"聚合后同步执行");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }).start();
});
System.out.println("mainx执行 ");

==== 结果:
mainx执行 
Thread[Thread-3,5,main]启动
Thread[Thread-4,5,main]启动
Thread[Thread-2,5,main]启动
Thread[Thread-1,5,main]启动
Thread[Thread-1,5,main]执行业务逻辑
Thread[Thread-0,5,main]启动
Thread[Thread-2,5,main]执行业务逻辑
Thread[Thread-4,5,main]执行业务逻辑
Thread[Thread-3,5,main]执行业务逻辑
Thread[Thread-0,5,main]执行业务逻辑
Thread[Thread-0,5,main]所有线程准备完毕,由最后一个线程执行此方法
Thread[Thread-0,5,main]执行聚合操作
Thread[Thread-0,5,main]聚合后同步执行
Thread[Thread-4,5,main]聚合后同步执行
Thread[Thread-2,5,main]聚合后同步执行
Thread[Thread-1,5,main]聚合后同步执行
Thread[Thread-3,5,main]聚合后同步执行
复制代码

Second, the thread counter -CountDownLatch

1. Definitions
  • Initialization mark number, await allows the current thread to block calls countDown the tag number minus 1, when the flag is 0, then wake up the blocked thread. You can wait for the other thread is finished executing.
2, Scene
  • After performing in the main thread needs to wait for the child thread is finished or need to wait for some time resources.
3, Code
CountDownLatch countThread=new CountDownLatch(5);

IntStream.range(0,5).forEach(i->{
    new Thread(()->{
        Thread.sleep(1000L);
        
        System.out.println(Thread.currentThread()+"执行逻辑");
        countThread.countDown();
    }).start();
});

System.out.println(Thread.currentThread()+"等待所有线程完毕");
countThread.await();
System.out.println(Thread.currentThread()+"继续");

==== 结果:
Thread[main,5,main]等待所有线程完毕
Thread[Thread-3,5,main]执行逻辑
Thread[Thread-0,5,main]执行逻辑
Thread[Thread-4,5,main]执行逻辑
Thread[Thread-1,5,main]执行逻辑
Thread[Thread-2,5,main]执行逻辑
Thread[main,5,main]继续
复制代码

Third, the signal amount -Semaphore

The number of threads simultaneously access a resource control

Guess you like

Origin juejin.im/post/5d79b2aa6fb9a06b0a27a1ca