zoukankan      html  css  js  c++  java
  • Java之线程池管理

    JDK5后建议使用ExecutorService与Excutors来创建与管理线程池, 不再建议直接使用Thread. 开始不明白原因, 今天知道结果了:
    使用Thread.currnetThread.join()后,线程卡着无法退出, 事实二个子线程已经完成了. 使用这个方法是等待子线程完成后再自行退出. 结果卡壳了.
    final FileCache cache = new FileCache(LoganalyseConfig.getInstance()).open();
      try {

       new Thread() {
        public void run() {
         try {
          testPut(cache);
         } catch (IOException e) {
          e.printStackTrace();
         }
        }
       }.start();

       new Thread() {
        public void run() {
         try {
          testGet(cache);
         } catch (IOException e) {
          e.printStackTrace();
         }
        }
       }.start();

       Thread.currentThread().join();
      } catch (InterruptedException e) {
       e.printStackTrace();
      } finally {
       if (cache != null) {
        cache.close();
       }
      }
    ==========================================================
    executor.submit(new Runnable() {
        public void run() {
         try {
          testPut(cache);
         } catch (IOException e) {
          e.printStackTrace();
         }
        }
       });

       executor.submit(new Runnable() {
        public void run() {
         try {
          testGet(cache);
         } catch (IOException e) {
          e.printStackTrace();
         }
        }
       });
       executor.shutdown();
       executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
       System.out.println("退出testPutGet()");

  • 相关阅读:
    Java GC系列(2):Java垃圾回收是如何工作的?
    BZOJ 2878 【NOI2012】 迷失游乐园
    BZOJ 2876 【NOI2012】 骑行川藏
    UOJ #126 【NOI2013】 快餐店
    BZOJ 3122 【SDOI2013】 随机数生成器
    BZOJ 1833 【ZJOI2010】 数字计数
    BZOJ 1269 【AHOI2006】 文本编辑器
    BZOJ 3930 【CQOI2015】 选数
    BZOJ 4569 【SCOI2016】 萌萌哒
    BZOJ 2756 【SCOI2012】 奇怪的游戏
  • 原文地址:https://www.cnblogs.com/zolo/p/5849314.html
Copyright © 2011-2022 走看看