概述
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//ExecutorService exec = Executors.newCachedThreadPool();
ExecutorService exec = Executors.newFixedThreadPool(2);
Future f = exec.submit(new TimeTask());
Future f2 = exec.submit(new TimeTask2());
try {
f.get(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
f.cancel(true);
// 定义超时后的状态修改
System.out.println("thread time out");
e.printStackTrace();
}
System.out.println("sfadsfasdfasdfasdfasdfasfd");
try {
f2.get(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
f.cancel(true);
// 定义超时后的状态修改
System.out.println("thread time out");
e.printStackTrace();
}
}
}
有时候中断不了
最后
以上就是隐形电话为你收集整理的mysql 线程池 超时_java 线程池 超时中断的全部内容,希望文章能够帮你解决mysql 线程池 超时_java 线程池 超时中断所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复