线程超时

package com.****.winUtliTest.ThreadTest;

import java.util.HashMap;
import java.util.concurrent.Callable; 
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;

/** 
* 启动一个任务,然后等待任务的计算结果,如果等待时间超出预设定的超时时间,则中止任务。 
* 
* @author Chen Feng 
*/ 
public class ThreadTest {

public static void main(String[] args) { 
// System.out.println("Start ..."); 
//
// ExecutorService exec = Executors.newCachedThreadPool(); //创建一个线程池 
// 
// testTask(exec, 15); // 任务成功结束后等待计算结果,不需要等到15秒 ntaskResult : true failReason: null
// testTask(exec, 5); // 只等待5秒,任务还没结束,所以将任务中止 ntaskResult : null failReason : 主线程等待计算结果超时,因此中断任务线程!
//
// exec.shutdown(); 
// System.out.println("End!"); 
}

public static HashMap<String, String> testTask(ExecutorService exec, int timeout) { 
// MyTask task = new MyTask(); 
a sA=new a();
Future<HashMap<String, String>> future = exec.submit(sA); 

HashMap<String, String> taskResult = null; 
String failReason = null; 
try { 
// 等待计算结果,最长等待timeout秒,timeout秒后中止任务 
taskResult = future.get(timeout, TimeUnit.SECONDS); 
} catch (InterruptedException e) { 
failReason = "主线程在等待计算结果时被中断!"; 
} catch (ExecutionException e) { 
failReason = "主线程等待计算结果,但计算抛出异常!"; 
} catch (TimeoutException e) { 
failReason = "主线程等待计算结果超时,因此中断任务线程!"; 
exec.shutdownNow(); 
} 

System.out.println("ntaskResult : " + taskResult.get("isSucc")); 
System.out.println("failReason : " + failReason);
return taskResult; 
} 
}

//class MyTask implements Callable<Boolean> { 
//
// @Override 
// public Boolean call() throws Exception { 
// // 总计耗时约10秒 
// for (int i = 0; i < 100L; i++) { 
// Thread.sleep(100); // 睡眠0.1秒 
// System.out.print('-'); 
// } 
// return Boolean.TRUE; 
// } 
//}

 

posted @ 2019-12-18 14:00  wang9264  阅读(547)  评论(0编辑  收藏  举报