把大的,复杂的任务分解成多个小任务,并行的处理,待所有线程结束后,返回结果。
/**
* 并行框架
* @author Administrator
*
*/
public class Executer {
//存储任务的执行结果
private List> futres = new ArrayList>();
//条件队列锁,以及线程计数器
public final Lock lock = new Lock();
//线程池
private ExecutorService pool = null;
public Executer() {
this(1);
}
public Executer(int threadPoolSize) {
pool = Executors.newFixedThreadPool(threadPoolSize);
}
/**
* 任务派发
* @param job
*/
public void fork(Job job){
//设置同步锁
job.setLock(lock);
//将任务派发给线程池去执行
futres.add(pool.submit(job));
//增加线程数
synchronized (lock) {
lock.thread_count++;
}
}
/**
* 统计任务结果
*/
public List join(){
synchronized (lock) {
while(lock.thread_count > 0){//检查线程数,如果为0,则表示所有任务处理完成
//System.out.println("threadCount: "+THREAD_COUNT);
try {
lock.wait();//如果任务没有全部完成,则挂起。等待完成的任务给予通知
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
List list = new ArrayList();
//取出每个任务的处理结果,汇总后返回
for (Future future : futres) {
try {
Object result = future.get();//因为任务都已经完成,这里直接get
if(result != null){
if(result instanceof List)
list.addAll((List)result);
else
list.add(result);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
}
/****************************************/
/**
* 抽象任务
* @author Administrator
*
*/
public abstract class Job implements Callable {
//锁
private Lock lock = null;
void setLock(Lock lock) {
this.lock = lock;
}
public Object call() throws Exception {
Object result = null;
try{
result = this.execute();//执行子类具体任务
}catch(Exception e){
e.printStackTrace();
}
synchronized (lock) {
//处理完业务后,任务结束,递减线程数,同时唤醒主线程
lock.thread_count--;
lock.notifyAll();
}
return result;
}
/**
* 业务处理函数
*/
public abstract Object execute();
}
/**************************************/
class Lock {
//线程数
int thread_count;
}
/*******************************************/
public class MyJob extends Job {
@Override
public User execute() {
//模拟业务需要处理1秒.
try {Thread.sleep(1000);} catch (InterruptedException e) {}
System.out.println("running thread id = "+Thread.currentThread().getId());
return new User();
}
}
/***************************************************************************/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
//初始化任务池
Executer exe = new Executer(5);
//初始化任务
long time = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
MyJob job = new MyJob();
exe.fork(job);//派发任务
}
//汇总任务结果
List list = exe.join();
System.out.println("ResultSize: "+list.size());
System.out.println("time: "+(System.currentTimeMillis() - time));
}
}