Utility Class
Semaphore
/**
* 5台机器,8个工人,一台机器只能被一个工人使用。
*/
public class Test {
public static void main(String[] args) {
int N = 8; //工人数
Semaphore semaphore = new Semaphore(5); //机器数目
for(int i=0;i<N;i++)
new Worker(i,semaphore).start();
}
static class Worker extends Thread{
private int num;
private Semaphore semaphore;
public Worker(int num,Semaphore semaphore){
this.num = num;
this.semaphore = semaphore;
}
@Override
public void run() {
try {
semaphore.acquire();
System.out.println("工人"+this.num+"占用一个机器在生产...");
Thread.sleep(2000);
System.out.println("工人"+this.num+"释放出机器");
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}CountDownLatch
CyclicBarrier
ThreadPoolExecutor
构造函数
核心逻辑
submit
Executors
Future
FutureTask

CompletableFuture
概念

创建 CompletableFuture
CompletionStage
串行关系
AND 汇聚关系
OR 汇聚关系
异常处理
CompletionService
Last updated
