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

 

首先是执行算法的CALL

Java代码 复制代码 收藏代码
  1. /**
  2. * <dl>
  3. * <dt><b>类功能概要</b></dt>
  4. * <dd></dd>
  5. * </dl>
  6. * Version        Date       Company   Developer    Revise
  7. * -------     ----------   ---------  ---------    ------
  8. * pisv2.3.2   2012-02-24   yihaodian   xiangqi     create
  9. */ 
  10. package com.yihaodian.pis.thread; 
  11.  
  12. import java.util.concurrent.Callable; 
  13.  
  14. /**
  15. * @author yhd
  16. *
  17. */ 
  18. class Content implements Callable<Integer> { 
  19.  
  20.     publicint data; 
  21.     public Integer call() throws Exception { 
  22.         data ++; 
  23.         //此段可以表示用来执行其他耗时任务 
  24.         Thread.sleep(5000l); 
  25.         return data; 
  26.     } 
  27.     publicint getData() { 
  28.         return data; 
  29.     } 
  30.     publicvoid setData(int data) { 
  31.         this.data = data; 
  32.     } 
  33.      
/**
 * <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;
	}
	
}

其次是执行这个计算的任务线程

Java代码 复制代码 收藏代码
  1. /**
  2. * <dl>
  3. * <dt><b>类功能概要</b></dt>
  4. * <dd></dd>
  5. * </dl>
  6. * Version        Date       Company   Developer    Revise
  7. * -------     ----------   ---------  ---------    ------
  8. * pisv2.3.2   2012-02-24   yihaodian   xiangqi     create
  9. */ 
  10. package com.yihaodian.pis.thread; 
  11.  
  12. import java.util.List; 
  13. import java.util.concurrent.BlockingQueue; 
  14. import java.util.concurrent.Callable; 
  15. import java.util.concurrent.Future; 
  16. import java.util.concurrent.LinkedBlockingQueue; 
  17. import java.util.concurrent.ThreadPoolExecutor; 
  18. import java.util.concurrent.TimeUnit; 
  19.  
  20. /**
  21. * @author yhd
  22. *
  23. */ 
  24. publicclass TestCall { 
  25.  
  26.     /**
  27.      * <dl>
  28.      * <dt><b>方法功能概要</b></dt>
  29.      * <dd></dd>
  30.      * </dl>
  31.      */ 
  32.     public Future<Integer> excute(Content content) { 
  33.         BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(); 
  34.         ThreadPoolExecutor executor = new ThreadPoolExecutor(50, 100, 1
  35.                 TimeUnit.MINUTES, workQueue, 
  36.                 new ThreadPoolExecutor.AbortPolicy()); 
  37.         return executor.submit(content); 
  38.     } 
  39.  
/**
 * <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);
	}

}

最后是主类,内部类设定参数,得到计算后的数据

Java代码 复制代码 收藏代码
  1. /**
  2. * <dl>
  3. * <dt><b>类功能概要</b></dt>
  4. * <dd></dd>
  5. * </dl>
  6. * Version        Date       Company   Developer    Revise
  7. * -------     ----------   ---------  ---------    ------
  8. * pisv2.3.2   2012-02-24   yihaodian   xiangqi     create
  9. */ 
  10. package com.yihaodian.pis.thread; 
  11.  
  12. import java.util.ArrayList; 
  13. import java.util.List; 
  14. import java.util.concurrent.BlockingQueue; 
  15. import java.util.concurrent.ExecutionException; 
  16. import java.util.concurrent.Future; 
  17. import java.util.concurrent.LinkedBlockingQueue; 
  18. import java.util.concurrent.ThreadPoolExecutor; 
  19. import java.util.concurrent.TimeUnit; 
  20.  
  21. /**
  22. * @author yhd
  23. *
  24. */ 
  25. publicclass Test { 
  26.  
  27.     /**
  28.      * <dl>
  29.      * <dt><b>方法功能概要</b></dt>
  30.      * <dd></dd>
  31.      * </dl>
  32.      */ 
  33.     publicstaticvoid main(String[] args) { 
  34.         BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(); 
  35.         ThreadPoolExecutor executor = new ThreadPoolExecutor(50, 100, 1
  36.                 TimeUnit.MINUTES, workQueue, 
  37.                 new ThreadPoolExecutor.AbortPolicy()); 
  38.         class MainThread implements Runnable { 
  39.             TestCall testCall = new TestCall(); 
  40.             @Override 
  41.             publicvoid run() { 
  42.                 try
  43.                 List<Future<Integer>> tasks = new ArrayList<Future<Integer>>(); 
  44.                 for (int i = 0; i < 1000; i++) { 
  45.                     Content content = new Content(); 
  46.                     content.setData(i); 
  47.                     tasks.add(testCall.excute(content)); 
  48.                      
  49.                 } 
  50.                 //结束循环,开始得到结果 
  51.                 //状态用来标记任务完成的状态 
  52.                 int status = 0
  53.                 while (status != 2) { 
  54.                     status = 1
  55.                     for(Future<Integer> task : tasks) { 
  56.                         if (!task.isDone()) { 
  57.                             status = 0
  58.                             //等待任务都完成 
  59.                             Thread.sleep(1000l); 
  60.                             break
  61.                         } 
  62.                         if (status == 1 || status == 2) { 
  63.                             System.out.println(task.get()); 
  64.                             status = 2
  65.                         } 
  66.                     } 
  67.                 } 
  68.                 } catch (Exception e) { 
  69.                     // TODO Auto-generated catch block 
  70.                     e.printStackTrace(); 
  71.                 } 
  72.             } 
  73.         } 
  74.         MainThread mainThread = new MainThread(); 
  75.         executor.submit(mainThread); 
  76.     } 
  77.  
/**
 * <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

posted @ 2013-01-12 19:49  attitudedecidesall  Views(543)  Comments(0Edit  收藏  举报