首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

多线程施用 任务执行 等待所有任务完成一起处理

2012-09-04 
多线程应用任务执行等待所有任务完成一起处理首先是执行算法的CALL/** * dl * dtb类功能概要/b/d

多线程应用 任务执行 等待所有任务完成一起处理

首先是执行算法的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> {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 * */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 *  */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();@Overridepublic 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 blocke.printStackTrace();}}}MainThread mainThread = new MainThread();executor.submit(mainThread);}}

?最后得到的结果就是?? 你想要的??? 2

热点排行