首先是执行算法的CALL
- /**
- * <dl>
- * <dt><b>类功能概要</b></dt>
- * <dd></dd>
- * </dl>
- * Version Date Company Developer Revise
- * ------- ---------- --------- --------- ------
- * pisv2.3.2 2012-02-24 yihaodian xiangqi create
- */
- package com.yihaodian.pis.thread;
- import java.util.concurrent.Callable;
- /**
- * @author yhd
- *
- */
- class Content implements Callable<Integer> {
- publicint data;
- public Integer call() throws Exception {
- data ++;
- //此段可以表示用来执行其他耗时任务
- Thread.sleep(5000l);
- return data;
- }
- publicint getData() {
- return data;
- }
- publicvoid setData(int data) {
- this.data = data;
- }
- }
/** * <dl> * <dt><b>类功能概要</b></dt> * <dd></dd> * </dl> * Version Date Company Developer Revise * ------- ---------- --------- --------- ------ * pisv2.3.2 2012-02-24 yihaodian xiangqi create */ package com.yihaodian.pis.thread; import java.util.concurrent.Callable; /** * @author yhd * */ class Content implements Callable<Integer> { public int data; public Integer call() throws Exception { data ++; //此段可以表示用来执行其他耗时任务 Thread.sleep(5000l); return data; } public int getData() { return data; } public void setData(int data) { this.data = data; } }
其次是执行这个计算的任务线程
- /**
- * <dl>
- * <dt><b>类功能概要</b></dt>
- * <dd></dd>
- * </dl>
- * Version Date Company Developer Revise
- * ------- ---------- --------- --------- ------
- * pisv2.3.2 2012-02-24 yihaodian xiangqi create
- */
- package com.yihaodian.pis.thread;
- import java.util.List;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.Callable;
- import java.util.concurrent.Future;
- import java.util.concurrent.LinkedBlockingQueue;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- /**
- * @author yhd
- *
- */
- publicclass TestCall {
- /**
- * <dl>
- * <dt><b>方法功能概要</b></dt>
- * <dd></dd>
- * </dl>
- */
- public Future<Integer> excute(Content content) {
- BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
- ThreadPoolExecutor executor = new ThreadPoolExecutor(50, 100, 1,
- TimeUnit.MINUTES, workQueue,
- new ThreadPoolExecutor.AbortPolicy());
- return executor.submit(content);
- }
- }
/** * <dl> * <dt><b>类功能概要</b></dt> * <dd></dd> * </dl> * Version Date Company Developer Revise * ------- ---------- --------- --------- ------ * pisv2.3.2 2012-02-24 yihaodian xiangqi create */ package com.yihaodian.pis.thread; import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * @author yhd * */ public class TestCall { /** * <dl> * <dt><b>方法功能概要</b></dt> * <dd></dd> * </dl> */ public Future<Integer> excute(Content content) { BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(); ThreadPoolExecutor executor = new ThreadPoolExecutor(50, 100, 1, TimeUnit.MINUTES, workQueue, new ThreadPoolExecutor.AbortPolicy()); return executor.submit(content); } }
最后是主类,内部类设定参数,得到计算后的数据
- /**
- * <dl>
- * <dt><b>类功能概要</b></dt>
- * <dd></dd>
- * </dl>
- * Version Date Company Developer Revise
- * ------- ---------- --------- --------- ------
- * pisv2.3.2 2012-02-24 yihaodian xiangqi create
- */
- package com.yihaodian.pis.thread;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.Future;
- import java.util.concurrent.LinkedBlockingQueue;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- /**
- * @author yhd
- *
- */
- publicclass Test {
- /**
- * <dl>
- * <dt><b>方法功能概要</b></dt>
- * <dd></dd>
- * </dl>
- */
- publicstaticvoid main(String[] args) {
- BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
- ThreadPoolExecutor executor = new ThreadPoolExecutor(50, 100, 1,
- TimeUnit.MINUTES, workQueue,
- new ThreadPoolExecutor.AbortPolicy());
- class MainThread implements Runnable {
- TestCall testCall = new TestCall();
- @Override
- publicvoid run() {
- try {
- List<Future<Integer>> tasks = new ArrayList<Future<Integer>>();
- for (int i = 0; i < 1000; i++) {
- Content content = new Content();
- content.setData(i);
- tasks.add(testCall.excute(content));
- }
- //结束循环,开始得到结果
- //状态用来标记任务完成的状态
- int status = 0;
- while (status != 2) {
- status = 1;
- for(Future<Integer> task : tasks) {
- if (!task.isDone()) {
- status = 0;
- //等待任务都完成
- Thread.sleep(1000l);
- break;
- }
- if (status == 1 || status == 2) {
- System.out.println(task.get());
- status = 2;
- }
- }
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- MainThread mainThread = new MainThread();
- executor.submit(mainThread);
- }
- }
/** * <dl> * <dt><b>类功能概要</b></dt> * <dd></dd> * </dl> * Version Date Company Developer Revise * ------- ---------- --------- --------- ------ * pisv2.3.2 2012-02-24 yihaodian xiangqi create */ package com.yihaodian.pis.thread; import java.util.ArrayList; import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * @author yhd * */ public class Test { /** * <dl> * <dt><b>方法功能概要</b></dt> * <dd></dd> * </dl> */ public static void main(String[] args) { BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(); ThreadPoolExecutor executor = new ThreadPoolExecutor(50, 100, 1, TimeUnit.MINUTES, workQueue, new ThreadPoolExecutor.AbortPolicy()); class MainThread implements Runnable { TestCall testCall = new TestCall(); @Override public void run() { try { List<Future<Integer>> tasks = new ArrayList<Future<Integer>>(); for (int i = 0; i < 1000; i++) { Content content = new Content(); content.setData(i); tasks.add(testCall.excute(content)); } //结束循环,开始得到结果 //状态用来标记任务完成的状态 int status = 0; while (status != 2) { status = 1; for(Future<Integer> task : tasks) { if (!task.isDone()) { status = 0; //等待任务都完成 Thread.sleep(1000l); break; } if (status == 1 || status == 2) { System.out.println(task.get()); status = 2; } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } MainThread mainThread = new MainThread(); executor.submit(mainThread); } }
最后得到的结果就是 你想要的 2