使用的是以下代码:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
/**
* 使用CompletionService解决Future的缺点
*
* @author xiaoshu
*/
public class Test1 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
MyCallable username1 = new MyCallable("username1", 5);
MyCallable username2 = new MyCallable("username2", 4);
MyCallable username3 = new MyCallable("username3", 3);
MyCallable username4 = new MyCallable("username4", 2);
MyCallable username5 = new MyCallable("username5", 1);
List<Callable> callables = new ArrayList<>();
callables.add(username1);
callables.add(username2);
callables.add(username3);
callables.add(username4);
callables.add(username5);
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
ExecutorCompletionService<Object> csRef = new ExecutorCompletionService<>(poolExecutor);
for (int i = 0; i < 5; i++) {
Future future = poolExecutor.submit(callables.get(i));
}
for (int i = 0; i < 5; i++) {
System.out.println("等待打印第" + (i + 1) + "个返回值");
System.out.println(csRef.take().get());
}
//System.out.println("main method End!");
}
}
class MyCallable implements Callable<String> {
private String username;
private long sleepValue;
public MyCallable(String username, long sleepValue) {
this.username = username;
this.sleepValue = sleepValue;
}
@Override
public String call() throws Exception {
System.out.println(username + " " + sleepValue);
Thread.sleep(sleepValue);
return "return " + username;
}
}
控制台输出如下:
从结果来看,就像是线程被阻塞了,一直获取不到结果,求解!!!