如果我运行长时间的任务,执行程序在第一个任务没有完成时不会启动新线程。请问有谁能帮我理解为什么会这样,以及如何解决?
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
public class TestExecutor {
@Test
public void test() throws InterruptedException {
ExecutorService checkTasksExecutorService = new ThreadPoolExecutor(1, 10,
100000, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
for (int i = 0; i < 20; i++) {
checkTasksExecutorService.execute(new Runnable() {
public void run(){
try {
System.out.println(Thread.currentThread().getName() + " running!");
Thread.sleep(10000);
} catch (Exception e) {
}
}
});
}
Thread.sleep(1000000);
}
}
ThreadPoolExecutor
类的Javadoc :) - Marko Topolnik