线程池管理类 Executors

来源:互联网 发布:酷盖是什么意思啊 知乎 编辑:程序博客网 时间:2024/04/29 19:52

    可以构建三种线程池,区别就在于执行子线程的时候有几个线程供子线程使用

       搞一个子线程类,如下:

public class MyThread implements Runnable {private int count=1;private int number;public MyThread(int num){this.number= num;System.out.println("创建线程"+num);}<pre name="code" class="java">public class CacheThreadPoll {public static void main(String[] args) {// TODO Auto-generated method stubExecutorService pool = Executors.newCachedThreadPool();for (int i = 0; i < 5; i++) {pool.execute(new MyThread(i));}pool.shutdown()}}@Overridepublic void run() {while(true){System.out.println("当前线程= "+Thread.currentThread()+" Thread-" + number + " run " + count+" time(s)");if(++count==3){return;}}}}


1、Executors.newCachedThreadPool()   根据需要创建新线程的线程池,特点:以前构造的线程可用时可以重用,用于短时异步的线程任务时,可以提高线程效率

public class CacheThreadPoll {public static void main(String[] args) {// TODO Auto-generated method stubExecutorService pool = Executors.newCachedThreadPool();for (int i = 0; i < 5; i++) {pool.execute(new MyThread(i));}pool.shutdown();}}

   执行后,显示的日志如下:

创建线程0创建线程1创建线程2当前线程= Thread[pool-1-thread-1,5,main]  Thread-0 run 1 time(s)创建线程3当前线程= Thread[pool-1-thread-2,5,main]  Thread-1 run 1 time(s)当前线程= Thread[pool-1-thread-3,5,main]  Thread-2 run 1 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-0 run 2 time(s)创建线程4当前线程= Thread[pool-1-thread-4,5,main]  Thread-3 run 1 time(s)当前线程= Thread[pool-1-thread-3,5,main]  Thread-2 run 2 time(s)当前线程= Thread[pool-1-thread-2,5,main]  Thread-1 run 2 time(s)当前线程= Thread[pool-1-thread-5,5,main]  Thread-4 run 1 time(s)当前线程= Thread[pool-1-thread-5,5,main]  Thread-4 run 2 time(s)当前线程= Thread[pool-1-thread-4,5,main]  Thread-3 run 2 time(s)  <span style="color:#ff0000;"> //有5个线程</span>
2、Executors.newFixedThreadPool(int num)   固定创建线程数量,num标识只能有几个线程使用

public class FixThreadPool {public static void main(String[] args) {// TODO Auto-generated method stubExecutorService pool = Executors.newFixedThreadPool(2);for (int i = 0; i < 5; i++) {pool.execute(new MyThread(i));}pool.shutdown();}}
创建线程0创建线程1创建线程2当前线程= Thread[pool-1-thread-2,5,main]  Thread-1 run 1 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-0 run 1 time(s)当前线程= Thread[pool-1-thread-2,5,main]  Thread-1 run 2 time(s)创建线程3创建线程4当前线程= Thread[pool-1-thread-2,5,main]  Thread-2 run 1 time(s)当前线程= Thread[pool-1-thread-2,5,main]  Thread-2 run 2 time(s)当前线程= Thread[pool-1-thread-2,5,main]  Thread-3 run 1 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-0 run 2 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-4 run 1 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-4 run 2 time(s)当前线程= Thread[pool-1-thread-2,5,main]  Thread-3 run 2 time(s)          <span style="color:#ff6666;">  //只有两个线程</span>
3、Executors.newSingleThreadExecutor()     只有一个线程

public class SingleThreadPoll {public static void main(String[] args) {// TODO Auto-generated method stubExecutorService pool = Executors.newSingleThreadExecutor();for (int i = 0; i < 5; i++) {pool.execute(new MyThread(i));}}}
创建线程0创建线程1创建线程2创建线程3创建线程4当前线程= Thread[pool-1-thread-1,5,main]  Thread-0 run 1 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-0 run 2 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-1 run 1 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-1 run 2 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-2 run 1 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-2 run 2 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-3 run 1 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-3 run 2 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-4 run 1 time(s)当前线程= Thread[pool-1-thread-1,5,main]  Thread-4 run 2 time(s)


备注:   在三种方案中,线程池内的线程个数是线程池管理的重要指标,根据需要建立不同的线程池

特别需要注意的是  构建线程池的类是ThreadPoolExecutor, 通过Executors对应的三个方法源码可以看到,ThreadPoolExecutor才是线程池的创造者





1 0
原创粉丝点击