`
bao231
  • 浏览: 86113 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

future 取消任务测试

 
阅读更多

ExecutorService es = Executors.newCachedThreadPool();

Future<?>  future = es.submit(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

try{

Thread.sleep(300000l);

System.out.println("begin");

for(int i=0;i<100000;i++){

if(Thread.currentThread().isInterrupted()){

System.err.println("Interrupted");

break;

}

System.out.println(i);

}

System.out.println("finish");

}catch (InterruptedException e) {

e.printStackTrace();

}catch (Exception e) {

e.printStackTrace();

}

}

});

Thread.sleep(1*1000l);

future.cancel(true);

System.out.println("interupt is send");

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics