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()");

  • 相关阅读:
    如何手动同步 Chrome 浏览器的数据
    如何释放 Windows 10 系统的 C 盘空间
    Foxmail
    常用 Git 命令
    常用的 .gitignore 模板
    MySQL InnoDB Engine--数据页存储和INSERT操作
    MySQL InnoDB Engine--数据页存储和ALTER TABLE操作
    MySQL InnoDB Engine--数据页存储
    MySQL Index--BAK和MRR演示
    MySQL Execute Plan--Index Merge特性
  • 原文地址:https://www.cnblogs.com/zolo/p/5849314.html
Copyright © 2011-2022 走看看