您的位置:首页 > 编程语言 > Java开发

java代码运行时间超时exception方法

2017-02-27 11:22 337 查看
final ExecutorService service = Executors.newSingleThreadExecutor();

Callable<String> theCode = new Callable<String>() {

public String call() {}

};

try {

final Future<?> future = service.submit (theCode);

sReturn = (String) future.get(4, TimeUnit.SECONDS);

}

catch (InterruptedException | ExecutionException | TimeoutException e) {

logger.warning("fail." + e.getMessage() + e.toString());

}

finally {

service.shutdown();

}

callable或runnable,使代码运行在另一个线程,主线程中计时控制而已,抛出timeout后theCode还可以继续运行直至结束,因为是两个不同的线程。

暂存未验证
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: