public void await() throws InterruptedException { };
调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行
public void countDown() { };
将count值减1
import java.util.concurrent.CountDownLatch;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;publicclassCountDownLatchDemo{static CountDownLatch downLatch =newCountDownLatch(10);publicstaticvoidmain(String[] args){
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(200);for(int i =10; i >0; i--){
executorService.submit(newTask());}try{
System.out.println("等待子线程执行完毕");
downLatch.await();//线程走到这里均挂起}catch(InterruptedException e){
e.printStackTrace();}
System.out.println("子线程执行完毕,继续执行其他");
executorService.shutdown();}staticclassTaskimplementsRunnable{@Overridepublicvoidrun(){
System.out.println(Thread.currentThread().getName()+"task is start");try{
Thread.sleep(2000);}catch(InterruptedException e){
e.printStackTrace();}
System.out.println(Thread.currentThread().getName()+"task is end");
downLatch.countDown();}}}
pool-1-thread-1task is start
pool-1-thread-3task is start
pool-1-thread-2task is start
pool-1-thread-4task is start
等待子线程执行完毕
pool-1-thread-5task is start
pool-1-thread-1task is end
pool-1-thread-3task is end
pool-1-thread-2task is end
pool-1-thread-4task is end
pool-1-thread-5task is end
子线程执行完毕,继续执行其他
CyclicBarrier(回环栅栏)
实现让一组线程等待至某个状态之后再全部同时执行
public CyclicBarrier(int parties, Runnable barrierAction) {}
import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;publicclassCylicBarrierDemo{static CyclicBarrier cyclicBarrier =newCyclicBarrier(5,newRunnable(){@Overridepublicvoidrun(){
System.out.println(Thread.currentThread().getName()+" i am cyclicBarrier last run");}});publicstaticvoidmain(String[] args){
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(200);for(int i =10; i >0; i--){
executorService.submit(newTask());}
executorService.shutdown();}staticclassTaskimplementsRunnable{@Overridepublicvoidrun(){
System.out.println(Thread.currentThread().getName()+"task is start");try{
Thread.sleep(2000);}catch(InterruptedException e){
e.printStackTrace();}try{
System.out.println(Thread.currentThread().getName()+"task is end");
cyclicBarrier.await();}catch(InterruptedException e){
e.printStackTrace();}catch(BrokenBarrierException e){
e.printStackTrace();}}}}import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;publicclassCylicBarrierDemo{static CyclicBarrier cyclicBarrier =newCyclicBarrier(5,newRunnable(){@Overridepublicvoidrun(){
System.out.println(Thread.currentThread().getName()+" i am cyclicBarrier last run");}});publicstaticvoidmain(String[] args){
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(200);for(int i =10; i >0; i--){
executorService.submit(newTask());}
executorService.shutdown();}staticclassTaskimplementsRunnable{@Overridepublicvoidrun(){
System.out.println(Thread.currentThread().getName()+"task is start");try{
Thread.sleep(2000);}catch(InterruptedException e){
e.printStackTrace();}try{
System.out.println(Thread.currentThread().getName()+"task is end");
cyclicBarrier.await();}catch(InterruptedException e){
e.printStackTrace();}catch(BrokenBarrierException e){
e.printStackTrace();}}}}
pool-1-thread-2task is start
pool-1-thread-1task is start
pool-1-thread-3task is start
pool-1-thread-4task is start
pool-1-thread-5task is start
pool-1-thread-6task is start
pool-1-thread-7task is start
pool-1-thread-8task is start
pool-1-thread-9task is start
pool-1-thread-10task is start
pool-1-thread-2task is end
pool-1-thread-1task is end
pool-1-thread-4task is end
pool-1-thread-3task is end
pool-1-thread-5task is end
pool-1-thread-5 i am cyclicBarrier last run
pool-1-thread-6task is end
pool-1-thread-7task is end
pool-1-thread-8task is end
pool-1-thread-9task is end
pool-1-thread-10task is end
pool-1-thread-10 i am cyclicBarrier last run
Semaphore
acquire() 获取一个许可
release() 释放一个许可
import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.Semaphore;publicclassSeamphoreDemo{static Semaphore seamphore =newSemaphore(5);publicstaticvoidmain(String[] args){
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(200);for(int i =10; i >0; i--){
executorService.submit(newTask());}
executorService.shutdown();}staticclassTaskimplementsRunnable{@Overridepublicvoidrun(){try{
seamphore.acquire();}catch(InterruptedException e){
e.printStackTrace();}
System.out.println(Thread.currentThread().getName()+"task is start");try{
Thread.sleep(2000);}catch(InterruptedException e){
e.printStackTrace();}
System.out.println(Thread.currentThread().getName()+"task is end");
seamphore.release();}}}
pool-1-thread-1task is start
pool-1-thread-2task is start
pool-1-thread-3task is start
pool-1-thread-4task is start
pool-1-thread-5task is start
pool-1-thread-1task is end
pool-1-thread-6task is start
pool-1-thread-2task is end
pool-1-thread-3task is end
pool-1-thread-4task is end
pool-1-thread-8task is start
pool-1-thread-9task is start
pool-1-thread-5task is end
pool-1-thread-7task is start
pool-1-thread-10task is start
pool-1-thread-6task is end
pool-1-thread-8task is end
pool-1-thread-9task is end
pool-1-thread-7task is end
pool-1-thread-10task is end
import java.util.concurrent.CountDownLatch;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;public class CountDownLatchDemo { static CountDownLatch downLatch = new CountDownLatch(10); public static void mai