java线程池简单应用

主要逻辑代码:

 

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);
  }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值