线程池,执行之后,某个方法执行超时。 public static void main(String[] args) throws IOException { int poolSize = 10; ExecutorService executorService = new ScheduledThreadPoolExecutor(poolSize); List list = new ArrayList(); list.add(new Callable() { public Object call() throws Exception { String result = "faliure"; try { result = test(result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return result; } return result; } }); Collection tasks = Collections.synchronizedCollection(list); try { List results = executorService.invokeAll(tasks, 3, TimeUnit.SECONDS); Future result; for (int i = 0; i < results.size(); i++) { result = (Future) results.get(i); System.out.println("result : " + result.isDone()); System.out.println("result : " + result.get()); } } catch (Exception e) { e.printStackTrace(); } finally { executorService.shutdown(); } } public static String test(String param) { String result = "huahua"; try { Thread.sleep(10000); } catch (Exception e) { e.printStackTrace(); return result; } return result; } |
|
20分 |
|
20分 |
线程都未跑完你就返回值啊,应该拿不到的呀。
|
解决了。不过感谢各位回答。
|