package t1;
import java.util.concurrent.*;
public class T2 {
// AbortPolicy 直接异常
// CallerRunsPolicy 回退
// DiscardPolicy 指出来max + 队列
public static void main(String[] args) {
ExecutorService executorService = new ThreadPoolExecutor(2,
5,
1L,
TimeUnit.SECONDS,
new LinkedBlockingDeque<Runnable>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.DiscardOldestPolicy());
try {
for (int i = 0; i < 20; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " 来了");
}
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
结果:
pool-1-thread-4 来了
pool-1-thread-2 来了
pool-1-thread-4 来了
pool-1-thread-4 来了
pool-1-thread-4 来了
pool-1-thread-2 来了
pool-1-thread-1 来了
pool-1-thread-5 来了
pool-1-thread-3 来了