线程池管理类 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
- 线程池管理类 Executors
- java多线程之 Executors线程池管理
- Executors 工具类 创建 的 线程池
- 线程池类Executors的运用
- 类 Executors创建线程池用法
- 使用Executors工厂类创建线程池
- JAVA线程池类Executors的使用
- ThreadPool 之 线程池工具类 Executors
- Java Executors(线程池)
- java Executors(线程池)
- Java Executors(线程池)
- Executors 和线程池
- Java Executors(线程池)
- Executors 与线程池
- Executors线程池--java
- 线程池 Executors 入门
- Java Executors(线程池)
- java Executors线程池
- PHP Mail
- Math的常用方法(用于操作数字)
- UVA 1658 (费用流)
- (原)浅谈C# runat="server"(新手值得注意)
- 在Ubuntu 15.10与fedora 23下修改mysql 5.7.11的编码方式为utf8
- 线程池管理类 Executors
- 时间戳转换
- java 注解的几大作用及使用方法详解(完)
- 密码算法详解——AES
- SpringMVC学习系列(6) 之 数据验证
- sql-"无法删除数据库,因为该数据库当前正在使用"问题解决
- Java设计模式之代理模式
- MongoDB 单表数据的导出和恢复例子
- 使用NSURLConnection 异步回调下载文件 下载文件的误区和解析