主要逻辑代码:
public class Work
{
private static final Logger log = Logger.getLogger(Work.class);
private boolean isEmpty = true;
private List<VchForId> drafts;
private long timeout;
private ICheckDraftService draftCheckService;
public Work()
{
this.timeout = PropertiesHelper.getProducerThreadWaitTimeout();
}
public void setDraftCheckService(ICheckDraftService draftCheckService)
{
this.draftCheckService = draftCheckService;
}
public synchronized void addTask() throws InterruptedException
{
while (true)
{
if (!(this.isEmpty)) {
log.info("任务未完成,暂停生产......");
super.wait();
}
log.info("生产线程开始生产");
this.drafts = this.draftCheckService.getWaitingForCheck();
if ((this.drafts == null) || (this.drafts.size() == 0)) {
log.info("数据库中不存在需要处理的任务,线程休眠[" + (this.timeout / 1000L) + "]秒......");
super.wait(this.timeout);
}
log.info("获取到[" + this.drafts.size() + "]笔任务");
this.draftCheckService.setDraftIsChecking(this.drafts);
this.isEmpty = false;
log.debug("唤醒消费线程......");
super.notifyAll();
log.debug("消费线程唤醒完毕......");
}
}
public synchronized void getTask()
throws InterruptedException
{
while (true)
{
if (this.isEmpty) {
log.info("无待处理任务,等待生产......");
super.wait();
}
log.info("消费线程开始处理任务......");
List taskList = new ArrayList(this.drafts.size());
Iterator it = this.drafts.iterator();
while (it.hasNext()) {
VchForId draft = (VchForId)it.next();
log.debug("待核验汇票:" + draft);
ICheckDraftService checkService = (ICheckDraftService)SpringHelper.getBean("draftCheckService");
checkService.setDraft(draft);
FutureTask task = new FutureTask(checkService);
taskList.add(task);
ThreadPoolHelper.getInstance().submit(task);
}
this.drafts = null;
join(taskList);
this.isEmpty = true;
log.debug("唤醒生产线程......");
super.notifyAll();
log.debug("生产线程唤醒完毕......");
}
}
private void join(List<Future> list)
{
try
{
for (Future task : list)
task.get();
} catch (ExecutionException e) {
log.error("等待线程池中所有任务处理完毕时发生异常!", e);
e.printStackTrace();
} catch (InterruptedException e) {
log.error("等待线程池中所有任务处理完毕时发生异常!", e);
e.printStackTrace();
}
}
public static void main(String[] args)
{
Work bean = (Work)SpringHelper.getBean("work");
Producer producer = new Producer(bean);
Consumer consumer = new Consumer(bean);
producer.start();
consumer.start();
}
class Consumer extends Thread
{
private Work work;
public Consumer(Work paramWork)
{
this.work = paramWork;
}
public void run()
{
try
{
this.work.getTask();
} catch (InterruptedException e) {
e.printStackTrace();
log.error(e);
}
}
}
class Producer extends Thread
{
private Work work;
public Producer(Work paramWork)
{
this.work = paramWork;
}
public void run()
{
try
{
this.work.addTask();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
线程池帮助类:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.log4j.Logger;
public class ThreadPoolHelper
{
private static final Logger log = Logger.getLogger(ThreadPoolHelper.class);
private static int nThreads = (PropertiesHelper.getThreadPoolSize().intValue() > 0) ? PropertiesHelper.getThreadPoolSize().intValue() : Runtime.getRuntime().availableProcessors();
private static ExecutorService exec;
private static ExecutorService rebuild()
{
log.warn("重新构建线程池……");
nThreads = (PropertiesHelper.getThreadPoolSize().intValue() > 0) ? PropertiesHelper.getThreadPoolSize().intValue() : Runtime.getRuntime().availableProcessors();
log.info("Thread Pool Size:" + nThreads);
exec = Executors.newFixedThreadPool(nThreads);
return exec;
}
public static ExecutorService getInstance() {
if (exec != null) {
return exec;
}
return rebuild();
}
static
{
log.info("Thread Pool Size:" + nThreads);
exec = Executors.newFixedThreadPool(nThreads);
}
}