Java并发包java.util.concurrent下提供了对线程池的支持,Java通过Executors提供四种线程池。
1.FixedThreadPool 定长线程池,可控制线程最大并发数,超出的线程会在队列中等待
package com.mina.executorservice;
import java.util.concurrent.TimeUnit;
/**
* @program: minaproject
* @description: 处理器
* @author: daiwenlong
* @create: 2018-09-01 14:27
**/
public class ThreadHandler implements Runnable {
private int count;
public ThreadHandler(int count){
this.count = count;
}
public void run() {
System.out.println(Thread.currentThread().getName()+"------count:"+count);
try {//使线程睡眠,模拟线程阻塞情况
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试
package com.mina.executorservice;
import java.util.concurrent.ExecutorService;
impor