java多线程处理业务

1、线程池

package com.chn.salary.reinforcededuct.thread;

import java.util.ArrayList; import java.util.List;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

public class ThreadPoolGen{

private List<Future> futureList=null;

private ExecutorService executorService=null;

public ThreadPoolGen(){  

executorService = Executors.newCachedThreadPool();

  futureList=new ArrayList<Future>();

}

public void addThread(Runnable thread){

  Future future =executorService.submit(thread);  

futureList.add(future);

}

public boolean isAllDone(){

  boolean isDone=true;  

for(int i=0;i<futureList.size();i++){

   if(!futureList.get(i).isDone()){

    isDone=false;

    return isDone;

   }

  }

  return isDone;

}

public void shutdown(){

  executorService.shutdown();

}

}

2、守护线程工厂(所有线程类在启动之前应该用该工厂设置成守护模式)

package com.chn.salary.reinforcededuct.factory;

import java.util.concurrent.ThreadFactory;

/** * 用于创建守护线程 * @author lwq * */ public class DaemonThreadFactory implements ThreadFactory { private static final DaemonThreadFactory daemonThreadFactory =new DaemonThreadFactory(); public static DaemonThreadFactory getInstance(){   return daemonThreadFactory; }

    public Thread newThread(Runnable r) {         Thread t = new Thread(r);         t.setDaemon(true);         return t;      }

}

3、线程类

package com.chn.salary.reinforcededuct.thread;

import java.util.List; import java.util.Map;

import com.chn.base.Constants;

import com.chn.base.context.ApplicationContext;

import com.chn.salary.application.action.model.reinforcededuct.TemporarySailorSalaryModel;

import com.chn.salary.application.action.model.reinforcededuct.UnitiveTemporarySalaryModel;

import com.chn.salary.reinforcededuct.TemporarySailorManager;

import com.chn.salary.reinforcededuct.monitor.IProgressMonitor;

public class UnitiveDeductThread implements Runnable{

private TemporarySailorManager tempSalaryManager = null;

int num = 0;

private List toProcessSailors = null;

private Map stopedSailorsCollection=null;

private UnitiveTemporarySalaryModel unitiveSalaryModel=null;

private IProgressMonitor progressMonitor=null;

public UnitiveDeductThread(List cutSailorListFromOracle,Map mStopedSailorsCollection,    UnitiveTemporarySalaryModel unitiveSalaryModel,IProgressMonitor theProgressMonitor) {

  if (cutSailorListFromOracle == null)

   this.num = 0;

  this.num = cutSailorListFromOracle.size();

  this.toProcessSailors = cutSailorListFromOracle;

  this.stopedSailorsCollection=mStopedSailorsCollection;

  this.unitiveSalaryModel=unitiveSalaryModel;

  tempSalaryManager=(TemporarySailorManager)ApplicationContext.getServiceFromContainer(     Constants.CONTAINER_NAME, "salary05:tempSalaryManager");

  this.progressMonitor=theProgressMonitor;

}

public void run(){     ....... }

}

4、业务处理

......

ListCutter listCutter = new ListCutter(validSailorNosInOracle, 400);

   ThreadPoolGen threadPool=new ThreadPoolGen();

   DaemonThreadFactory daemonThreadFactory =DaemonThreadFactory.getInstance();

   for (int i = 0; i < listCutter.getCutList().size(); i++) {

    List sailorList =listCutter.getCutList().get(i);

UnitiveDeductThread runable=new UnitiveDeductThread(sailorList, stopedSailorsCollection,       tempSalaryModel,progressMonitor);     threadPool.addThread(daemonThreadFactory.newThread(runable));

   }

      while(true){

    try {

     Thread.sleep(2500);

    } catch (Exception e) {

     // TODO: handle exception

    }

      if(threadPool.isAllDone()){

     threadPool.shutdown();

     break;

    }

   }

......

原文地址:https://www.cnblogs.com/hy928302776/p/3080897.html