import java.util.concurrent.*;
/**
* 自定义线程创建
*/
public class RejectThreadPoolDemo2 {
public static class MyTask implements Runnable{
@Override
public void run() {
System.out.println(System.currentTimeMillis()+" Id:"+Thread.currentThread().getId());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException{
MyTask task = new MyTask();
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 0L,
TimeUnit.MILLISECONDS, new SynchronousQueue<>(),//SynchronousQueue没有容量,每一个插入操作都要等待一个相应的删除操作
new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
System.out.println("create "+thread);
return thread;
}
}
);
for (int i = 0; i < 5; i++) {
executor.submit(task);
}
}
//create Thread[Thread-0,5,main]
//create Thread[Thread-1,5,main]
//1567646705048 Id:9
//create Thread[Thread-2,5,main]
//1567646705048 Id:10
//create Thread[Thread-3,5,main]
//1567646705049 Id:11
//create Thread[Thread-4,5,main]
//1567646705049 Id:12
//1567646705050 Id:13
}
转载于:https://www.cnblogs.com/fly-book/p/11463190.html