package com.zxz.pgdemo.controller;
import java.util.concurrent.*;
public class RejectHandlerDemo {
public static class MyTask implements Runnable {
public Object data;
public CountDownLatch countDownLatch;
public MyTask(Object data, CountDownLatch countDownLatch) {
this.data=data;
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
doWork(data);
countDownLatch.countDown();
}
public void doWork(Object data) {
System.out.println(System.currentTimeMillis() + ":Thread ID:" + Thread.currentThread().getId() + ";data=" + data.toString());
}
}
public static void main(String args[]) throws InterruptedException {
LinkedBlockingDeque<Runnable> queue = new LinkedBlockingDeque<>(10);
ExecutorService executorService = new ThreadPoolExecutor(5, 5,
0L, TimeUnit.SECONDS,
queue,
Executors.defaultThreadFactory()
, (r, executor) -> {
try {
queue.put(r);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(r.toString() + " is discard");
}
});
CountDownLatch countDownLatch = new CountDownLatch(100);
for (int i = 0; i < 100; i++) {
executorService.submit(new MyTask(i, countDownLatch));
Thread.sleep(10);
}
countDownLatch.await();
System.out.println("---------done--------");
executorService.shutdown();
}
}
RejectThreadPoolDemo
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...