【发布时间】:2017-06-14 18:57:01
【问题描述】:
我正在尝试从主线程并行执行 2 个作业,但如果回调方法需要很长时间才能给出响应,其余请求将暂停并等待首先完成。
这是我的代码:
private final ExecutorService executorService = Executors.newFixedThreadPool(10);
private void executeService(String uuid) {
System.out.println("query executed done: " + uuid);
}
private String getAsynchTest(final String uuid) throws Exception {
testAsynchF = executorService.submit(
new Callable<String>() {
public String call() throws Exception {
executeService(uuid);
return getFutuerResult(uuid, Thread.currentThread()); // long processing
}
});
return testAsynchF.get();
}
public String getFutuerResult(String uuid, Thread t) {
String dummy = "your result for request: "+uuid;
if (uuid.equalsIgnoreCase("112")) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return dummy;
}
public static void main(String[] args) {
SynchronousTimeoutTester tester = new SynchronousTimeoutTester();
try {
String one = "112"
System.out.println("Result sync call:*** " + tester.getAsynchTest(one));
String two = "115";
System.out.println("Result sync call:**** " + tester.getAsynchTest(two));
} catch (Exception e) {
System.out.println("catched as Exception: " + e);
}
}
如果请求 112 线程暂停,为什么会停止执行请求 115?
【问题讨论】:
-
这不会编译...具体是什么
executeService(String)和getFutureResult(String)的实现? -
executeService 方法未列出,getFutuerResult 需要一个 Thread 参数,main 中有一行缺少分号
-
tester.shutdown() 也未指定
-
删除关闭方法调用..问题更新
标签: java multithreading future executorservice