【发布时间】:2017-08-31 11:40:51
【问题描述】:
我有一个 ThreadPoolExecutor -corePoolSize=5, maxPoolSize=10 queueSize = 10,keepAlive=1000 秒。我正在执行100 Runnable 任务。实际执行的任务数量是变化的,并不是所有的都被执行。 RejectionHandler 也没有报告任何内容。我相信我对 ThreadPoolExecutor 的理解在某种程度上是错误的。有谁能够帮我?我怎样才能让所有的任务都被执行?
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestThreadPoolExecutor {
public static void main(String[] args) {
ThreadFactory threadFactory = Executors.defaultThreadFactory();
ArrayBlockingQueue<Runnable> arrayBlockingQueue = new ArrayBlockingQueue<Runnable>(10);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10,1000, TimeUnit.SECONDS, arrayBlockingQueue, threadFactory, new RejectedExecutionHandlerImpl());
MonitorThread monitor = new MonitorThread(threadPoolExecutor, 3);
Thread monitorThread = new Thread(monitor);
monitorThread.start();
for (int i = 0; i < 100; i++) {
threadPoolExecutor.execute(new DummyRunnableTask(i));
}
//threadPoolExecutor.shutdown();
//monitor.shutDown();
}
}
class DummyRunnableTask implements Runnable {
private int i;
public DummyRunnableTask(int i) {
super();
this.i = i;
}
@Override
public void run() {
/*try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
System.out.println("Thread Name:=" + Thread.currentThread().getName()+ " is working for id=" + i);
}
}
class RejectedExecutionHandlerImpl implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println();
}
}
class MonitorThread implements Runnable{
private ThreadPoolExecutor executor;
private int seconds;
private Boolean run = true;
public MonitorThread(ThreadPoolExecutor executor, int seconds) {
super();
this.executor = executor;
this.seconds = seconds;
}
public void shutDown() {
this.run = false;
}
@Override
public void run() {
while (run) {
System.out.println(
String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
this.executor.getPoolSize(),
this.executor.getCorePoolSize(),
this.executor.getActiveCount(),
this.executor.getCompletedTaskCount(),
this.executor.getTaskCount(),
this.executor.isShutdown(),
this.executor.isTerminated()));
try {
Thread.sleep(seconds*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
样本输出:
...............
Thread Name:=pool-1-thread-7 is working for id=97
Thread Name:=pool-1-thread-2 is working for id=95
Thread Name:=pool-1-thread-10 is working for id=94
Thread Name:=pool-1-thread-4 is working for id=93
[monitor] [0/5] Active: 0, Completed: 0, Task: 1, isShutdown: false, isTerminated: false
[monitor] [10/5] Active: 0, Completed: 88, Task: 88, isShutdown: false, isTerminated: false
[monitor] [10/5] Active: 0, Completed: 88, Task: 88, isShutdown: false, isTerminated: false
【问题讨论】:
-
你期待什么输出?
-
我希望打印 0~99,这让我确认所有 100 个任务都已执行。截至目前,有时它会打印出一些随机数,而不是全部。
-
这是 (Link) 对您问题的回答
-
由于线程的执行是依赖于JVM的,所以你不能指望那里的执行顺序
标签: java multithreading