san*_*hat 3 java concurrency multithreading
在我探索的过程中ExecutorService,我遇到了一种Future.get()接受它的方法timeout.
这种方法的Java文档说
如果需要,最多等待计算完成的给定时间,然后检索其结果(如果可用).
参数:
超时等待的最长时间
单位超时参数的时间单位
根据我的理解,我们正在施加超时callable,我们提交给ExecutorService我,以便在指定的时间(超时)过后我callable会中断
但是根据下面的代码,longMethod()似乎超出了超时(2秒),我真的很难理解这一点.任何人都可以请我指出正确的道路吗?
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Timeout implements Callable<String> {
public void longMethod() {
for(int i=0; i< Integer.MAX_VALUE; i++) {
System.out.println("a");
}
}
@Override
public String call() throws Exception {
longMethod();
return "done";
}
/**
* @param args
*/
public static void main(String[] args) {
ExecutorService service = Executors.newSingleThreadExecutor();
try {
service.submit(new Timeout()).get(2, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Eug*_*ene 12
我的callable将在指定的时间(超时)过后中断
不对.任务将继续执行,而在超时后您将有一个空字符串.
如果你想取消它:
timeout.cancel(true) //Timeout timeout = new Timeout();
Run Code Online (Sandbox Code Playgroud)
PS正如你现在所拥有的,这个中断将不会产生任何影响.你没有以任何方式检查它.
例如,此代码考虑了中断:
private static final class MyCallable implements Callable<String>{
@Override
public String call() throws Exception {
StringBuilder builder = new StringBuilder();
try{
for(int i=0;i<Integer.MAX_VALUE;++i){
builder.append("a");
Thread.sleep(100);
}
}catch(InterruptedException e){
System.out.println("Thread was interrupted");
}
return builder.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
ExecutorService service = Executors.newFixedThreadPool(1);
MyCallable myCallable = new MyCallable();
Future<String> futureResult = service.submit(myCallable);
String result = null;
try{
result = futureResult.get(1000, TimeUnit.MILLISECONDS);
}catch(TimeoutException e){
System.out.println("No response after one second");
futureResult.cancel(true);
}
service.shutdown();
Run Code Online (Sandbox Code Playgroud)