【发布时间】:2017-09-28 02:49:31
【问题描述】:
我想使用多线程方法编写一个程序,其中每个线程执行一些 IO 操作,每个 IO 操作需要 100 毫秒才能完成。我的问题是我想这样设计我的问题,即一旦线程调用 IO 操作并进入等待状态,其余线程应该开始执行。
这是我最初的实验:
public class TestThread {
public static void main(String args[]) throws InterruptedException {
List<String> list = new ArrayList<>();
ExecutorService executorService = Executors.newFixedThreadPool(10);
for(int i = 0; i < 50; i++) {
Future<String> future = executorService.submit(() -> {
System.out.println(Thread.currentThread().getId());
return getValue();
//here instead of calling getValue(), thread will call some IO operation
//each IO call is indepndent of each other
});
list.add(future.get());
}
}
static String getValue() throws InterruptedException {
if(Thread.currentThread().getId() == 11)
Thread.sleep(10000);
return "some value for the thread " + Thread.currentThread().getId();
}
}
观察到的情况如下: 12 号线程一直等到 11 号线程完成执行。我想要的是,一旦 11 号线程进入睡眠状态(在实际问题中它将进入 IO 等待),12 号线程应该开始执行。 请帮我设计这个问题。
【问题讨论】:
-
什么?请尝试重申您的问题。
-
你有一个固定的线程池。当然,它会等待。如果线程池小于线程数,就会阻塞。
标签: java multithreading threadpool threadpoolexecutor