FutureTask獲取執行緒返回值
阿新 • • 發佈:2019-02-13
定義執行緒池:
private static ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); thread.setName("TEST-THREAD"
定義執行緒:
private class TestThread implements Callable<Boolean> { private int id; public TestThread(int id) { this.id = id; } @Override public Boolean call() throws Exception { System.out.println("執行執行緒id:" + id); try
test方法:
public static void main(String[] args) throws Exception { List<FutureTask<Boolean>> taskList = Lists.newArrayList(); // 非同步執行 for (int