submit方法会产生Future对象,它用Callable返回结果的特定类型进行了参数化,可以用isDone()的方法来查询Future是否已经完成。当任务完成是,它具有一个结果,可以调用get()方法来获取该结果。
/** * @Title: TaskWithResult.java * @Package zeze * @Description: TODO(用一句话描述该文件做什么) * @author A18ccms A18ccms_gmail_com * @date 2017年2月8日 上午10:51:26 * @version V1.0 */ package zeze; import java.util.ArrayList; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class CallableDemo { public static void main(String[] args) { ExecutorService exec = Executors.newCachedThreadPool(); ArrayList<Future<String>> results = new ArrayList<Future<String>>(); for (int i = 0; i < 10; i++) { results.add(exec.submit(new TaskWithResult(i))); } for (Future<String> fs : results) { try { System.out.println(fs.get()); } catch (Exception e) { System.err.println(e); } finally { exec.shutdown(); } } } } class TaskWithResult implements Callable<String> { private int id; public TaskWithResult(int id) { this.id = id; } @Override public String call() { return "result of TaskWithResult " + id; } }
用isDone()的方法来查询Future是否已经完成
boolean isDone = false; while (!isDone) { logger.info(Thread.currentThread().getName() + " 线程是否结束?"+ runningFlag+"; 队列大小=" + nextDepthQueue.size()); isDone = true; for (Future<Object> future : futureList) { if (!future.isDone()) { isDone = false; try { Thread.sleep(3000); } catch (InterruptedException e) { } break; } } }
改进版:
package test; import java.util.ArrayList; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /*** * * @ClassName: EbayMultiplyThreadCheck * @Description: TODO * @author zeze * @date 2017年2月16日 上午8:49:46 * */ public class EbayMultiplyThreadCheck { public static void main(String[] args) { ExecutorService exec = Executors.newFixedThreadPool(10); ArrayList<Future<String>> results = new ArrayList<Future<String>>(); for (int i = 0; i < 10; i++) { String email = "asd" + i + "@qq.com"; results.add(exec.submit(new TaskWithResult(email))); } boolean isDone = false; while (!isDone) { isDone = true; for (Future<String> future : results) { if (!future.isDone()) { isDone = false; try { Thread.sleep(1000); } catch (InterruptedException e) { } break; } } } exec.shutdown(); } } class TaskWithResult implements Callable<String> { private String email; public TaskWithResult(String email) { this.email = email; } @Override public String call() { System.out.println(email); return null; } }
Runnable 实现:
/** * @Title: RunnableDemo.java * @Package zeze * @Description: TODO(用一句话描述该文件做什么) * @author A18ccms A18ccms_gmail_com * @date 2017年3月13日 下午4:08:09 * @version V1.0 */ package zeze; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import javafx.animation.Animation.Status; /** * @ClassName: RunnableDemo * @Description: TODO * @author zeze * @date 2017年3月13日 下午4:08:09 * */ public class RunnableDemo implements Runnable { protected int countDown = 10; private static int taskCount = 0; private final int id = taskCount++; public RunnableDemo() { } public RunnableDemo(int countDown) { this.countDown = countDown; } public String status() { return "#" + id + "(" + (countDown > 0 ? countDown : "LiftOff!") + ")."; } @Override public void run() { while (countDown-- > 0) { System.out.print(status()); Thread.yield();// 将CPU从一个线程转移给另一个线程 } } public static void main(String[] args) { System.out.println("这个任务的run()不是单独的线程驱动,是在main()中直接调用"); RunnableDemo launch = new RunnableDemo(); launch.run(); System.out.println(); System.out.println("************************************"); System.out.println("在新线程中启动任务"); Thread thread = new Thread(new RunnableDemo()); thread.start(); System.out.println("Waiting for LiftOff"); System.out.println("************************************"); System.out.println("添加多个线程去驱动更多的任务"); for (int i = 0; i < 5; i++) { new Thread(new RunnableDemo()).start(); } System.out.println("Waiting for LiftOff"); System.out.println("************************************"); System.out.println("使用executor"); ExecutorService exec = Executors.newCachedThreadPool(); exec=Executors.newFixedThreadPool(5); for (int i = 0; i < 5; i++) { exec.execute(new RunnableDemo()); } exec.shutdown(); } }