>>分享Java编程技术,对《Java面向对象编程》等书籍提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 30875 个阅读者 刷新本主题
 * 贴子主题:  Java:使用Executors创建和管理线程池 回复文章 点赞(0)  收藏  
作者:flybird    发表时间:2020-01-08 07:49:10     消息  查看  搜索  好友  邮件  复制  引用

1. 类  Executors

  此类中提供的一些方法有:

1.1 public static ExecutorService newCachedThreadPool()

  创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们。对于执行很多短期异步任务的程序而言,这些线程池通常可提高程序性能。

1.2 public static ExecutorService newFixedThreadPool(int nThreads)

创建一个可重用固定线程数的线程池,以共享的×××队列方式来运行这些线程。

1.3 public static ExecutorService newSingleThreadExecutor()

  创建一个使用单个  worker  线程的  Executor ,以×××队列方式来运行该线程。

这三个方法都可以配合接口 ThreadFactory 的实例一起使用。并且返回一个 ExecutorService 接口的实例。

2. 接口  ThreadFactory

  根据需要创建新线程的对象。使用线程工厂就无需再手工编写对  new

Thread  的调用了,从而允许应用程序使用特殊的线程子类、属性等等。  

  此接口最简单的实现就是:  

   class SimpleThreadFactory implements  ThreadFactory {

                 public Thread newThread(Runnable r) {

                   return new Thread(r);

                 }

      }    

3. 接口 ExecutorService  

  该接口提供了管理终止的方法。

4. 创建标准线程池启动线程  

   4.1   提供一个简单的实现  Runnable  接口的线程  

//   MyThread.java  

     package  com.zj.concurrency.executors;  

     public   class  MyThread implements  Runnable {  

         private   int   count  = 1,  number ;  

          public  MyThread( int  num) {  

             number  = num;  

             System.  out .println( "Create Thread-"  +  number );  

         }  

         public   void  run() {  

             while  ( true ) {  

                System.  out .println( "Thread-"  +  number  +  " run "  +  count + " time(s)" );  

                 if  (++ count  == 3)  

                   return ;  

            }  

         }  

    }    

  这个线程会打印出相应的创建和执行信息。

   4.2  使用  CachedThreadPool  启动线程  

//CachedThreadPool.java  

   package  com.zj.concurrency.executors;  

   import  java.util.concurrent.ExecutorService;  

   import  java.util.concurrent.Executors;  

   public   class  CachedThreadPool {  

         public   static   void  main(String[] args) {  

            ExecutorService exec = Executors. newCachedThreadPool();  

             for  ( int  i = 0; i < 5; i++)  

                exec.execute( new  MyThread(i));  

            exec.shutdown();  

         }  

    }    

  结果:

  Create Thread-0  

  Create Thread-1  

  Create Thread-2  

  Create Thread-3  

  Thread-0 run 1 time(s)  

  Thread-0 run 2 time(s)  

  Thread-1 run 1 time(s)  

  Thread-1 run 2 time(s)  

  Thread-2 run 1 time(s)  

  Thread-2 run 2 time(s)  

  Create Thread-4  

  Thread-4 run 1 time(s)  

  Thread-4 run 2 time(s)  

  Thread-3 run 1 time(s)  

  Thread-3 run 2 time(s)

4.3   使用  FixedThreadPool  启动线程  

//FixedThreadPool.java  

package  com.zj.concurrency.executors;  

import  java.util.concurrent.ExecutorService;  

import  java.util.concurrent.Executors;  

public   class  FixedThreadPool {  

         public   static   void  main(String[] args) {  

            ExecutorService exec = Executors. newFixedThreadPool(2);  

             for  ( int  i = 0; i < 5; i++)  

                exec.execute( new  MyThread(i));  

            exec.shutdown();  

         }  

    }    

  结果:

  Create Thread-0  

  Create Thread-1  

  Create Thread-2  

  Create Thread-3  

  Create Thread-4  

  Thread-0 run 1 time(s)  

  Thread-0 run 2 time(s)  

  Thread-2 run 1 time(s)  

  Thread-2 run 2 time(s)  

  Thread-3 run 1 time(s)  

  Thread-3 run 2 time(s)  

  Thread-4 run 1 time(s)  

  Thread-4 run 2 time(s)  

  Thread-1 run 1 time(s)  

  Thread-1 run 2 time(s)

4.4   使用  SingleThreadExecutor  启动线程  

//   SingleThreadExecutor.java  

package  com.zj.concurrency.executors;  

import  java.util.concurrent.ExecutorService;  

import  java.util.concurrent.Executors;  

public   class  SingleThreadExecutor {  

         public   static   void  main(String[] args) {  

            ExecutorService exec = Executors. newSingleThreadExecutor();  

             for  ( int  i = 0; i < 5; i++)  

                exec.execute( new  MyThread(i));  

            exec.shutdown();  

         }  

    }    

  结果:

  Create Thread-0  

  Create Thread-1  

  Create Thread-2  

  Create Thread-3  

  Create Thread-4  

  Thread-0 run 1 time(s)  

  Thread-0 run 2 time(s)  

  Thread-1 run 1 time(s)  

  Thread-1 run 2 time(s)  

  Thread-2 run 1 time(s)  

  Thread-2 run 2 time(s)  

  Thread-3 run 1 time(s)  

  Thread-3 run 2 time(s)  

  Thread-4 run 1 time(s)  

  Thread-4 run 2 time(s)

5. 配合 ThreadFactory 接口的使用  

  我们试图给线程加入 daemon 和 priority 的属性设置。

   5.1  设置后台线程属性  

//DaemonThreadFactory.java    
package  com.zj.concurrency.executors.factory;  

import  java.util.concurrent.ThreadFactory;  

public   class  DaemonThreadFactory implements  ThreadFactory {  

public  Thread newThread(Runnable r)

  {  

            Thread t = new  Thread(r);  

            t.setDaemon( true );  

             return  t;  

         }  

    }    

                 5.2   设置优先级属性  

   最高优先级 :MaxPriorityThreadFactory.java  

     package  com.zj.concurrency.executors.factory;  

   import  java.util.concurrent.ThreadFactory;  

    public   class  MaxPriorityThreadFactory implements  ThreadFactory {  

         public  Thread newThread(Runnable r)  {  

            Thread t = new  Thread(r);  

            t.setPriority(Thread.  MAX_PRIORITY );  

             return  t;  

         }  

    }    

[code]   最低优先级:  MinPriorityThreadFactory.java    

[code]    package  com.zj.concurrency.executors.factory;  

   import  java.util.concurrent.ThreadFactory;  

    public   class  MinPriorityThreadFactory implements  ThreadFactory {  

         public  Thread newThread(Runnable r)  {  

            Thread t = new  Thread(r);  

            t.setPriority(Thread.  MIN_PRIORITY );  

             return  t;  

         }  

    }    

               5.3  启动带有属性设置的线程  

  /* ExecFromFactory.java    */
  package  com.zj.concurrency.executors;  

   import  java.util.concurrent.ExecutorService;  

   import  java.util.concurrent.Executors;  

   import   com.zj.concurrency.executors.factory.DaemonThreadFactory;  

   import   com.zj.concurrency.executors.factory.MaxPriorityThreadFactory;  

   import    com.zj.concurrency.executors.factory.MinPriorityThreadFactory;  

public   class  ExecFromFactory {  

         public   static   void  main(String[] args) throws  Exception  {  

            ExecutorService defaultExec =  Executors. newCachedThreadPool();  

            ExecutorService daemonExec = Executors  

                           . newCachedThreadPool( new   DaemonThreadFactory());  

            ExecutorService maxPriorityExec =  Executors . newCachedThreadPool(

                                                                     new MaxPriorityThreadFactory());  

            ExecutorService minPriorityExec = Executors  

                   . newCachedThreadPool( new  MinPriorityThreadFactory());  

             for  ( int  i = 0; i < 10; i++)  

                daemonExec.execute( new  MyThread(i));  

             for  ( int  i = 10; i < 20; i++)  

                 if  (i == 10)  

                   maxPriorityExec.execute( new  MyThread(i));  

                 else   if  (i == 11)  

                   minPriorityExec. execute ( new  MyThread(i));

                 else  

                    defaultExec.execute( new  MyThread(i));  

         }  

    }    

  结果:

  Create Thread-0  

  Create Thread-1  

  Create Thread-2  

  Create Thread-3  

  Thread-0 run 1 time(s)  

  Thread-0 run 2 time(s)  

  Thread-1 run 1 time(s)  

  Thread-1 run 2 time(s)  

  Thread-2 run 1 time(s)  

  Thread-2 run 2 time(s)  

  Create Thread-4  

  Thread-4 run 1 time(s)  

  Thread-4 run 2 time(s)  

  Create Thread-5  

  Thread-5 run 1 time(s)  

  Thread-5 run 2 time(s)  

  Create Thread-6  

  Create Thread-7  

  Thread-7 run 1 time(s)  

  Thread-7 run 2 time(s)  

  Create Thread-8  

  Thread-8 run 1 time(s)  

  Thread-8 run 2 time(s)  

  Create Thread-9  

  Create Thread-10  

  Thread-10 run 1 time(s)  

  Thread-10 run 2 time(s)  

  Create Thread-11  

  Thread-9 run 1 time(s)  

  Thread-9 run 2 time(s)  

  Thread-6 run 1 time(s)  

  Thread-6 run 2 time(s)  

  Thread-3 run 1 time(s)  

  Thread-3 run 2 time(s)  

  Create Thread-12  

  Create Thread-13  

  Create Thread-14  

  Thread-12 run 1 time(s)  

  Thread-12 run 2 time(s)  

  Thread-13 run 1 time(s)  

  Thread-13 run 2 time(s)  

  Create Thread-15  

  Thread-15 run 1 time(s)  

  Thread-15 run 2 time(s)  

  Create Thread-16  

  Thread-16 run 1 time(s)  

  Thread-16 run 2 time(s)  

  Create Thread-17  

  Create Thread-18  

  Create Thread-19  

  Thread-14 run 1 time(s)  

  Thread-14 run 2 time(s)  

  Thread-17 run 1 time(s)  

  Thread-17 run 2 time(s)  

  Thread-18 run 1 time(s)  

  Thread-18 run 2 time(s)  

  Thread-19 run 1 time(s)  

  Thread-19 run 2 time(s)  

  Thread-11 run 1 time(s)  

  Thread-11 run 2 time(s)  

----------------------------
原文链接:https://blog.51cto.com/zhangjunhd/70068

程序猿的技术大观园:www.javathinker.net




[这个贴子最后由 sunweiqin 在 2020-01-11 21:06:28 重新编辑]
  Java面向对象编程-->Java常用类(下)
  JavaWeb开发-->内部类
  JSP与Hibernate开发-->Servlet技术详解(Ⅲ)
  Java网络编程-->JSP技术详解(Ⅱ)
  精通Spring-->映射组成关系
  Vue3开发-->映射组成关系
  Doubles swept by Braves, set out 14 times in 3-0 loss
  Java设计模式: 里氏替换原则和合成复用原则详解
  用VisualVM远程监控Java进程
  我是如何把一个15分钟的程序优化到了10秒的
  面试问我,创建多少个线程合适?我该怎么说
  请求大佬们的帮助
  Java是如何实现自己的SPI机制的?
  编程语言搜索量排行:用十年数据告诉你什么最受欢迎
  正则表达式:运算符优先级
  redis持久化问题处理
  Java入门实用代码: List 列表中元素的替换
  Java入门实用代码:修改链表LinkedList
  Java 入门实用代码:汉诺塔算法
  Java 入门实用代码: 数组差集
  通过java.net.URL类连接HTTP服务器时获取响应结果的头部信息
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。