java 线程池(ThreadPoolExecutor)
来源:互联网 发布:步步高录音电话软件 编辑:程序博客网 时间:2024/04/29 22:32
在Executors类里面提供了一些静态工厂,生成一些常用的线程池。
1. newSingleThreadExecutor
创建一个单线程的线程池。这个线程池只有一个线程在工作,如果这个唯一的线程因为异常结束,那么会有一个新的线程来替代它。此线程池保证所有任务的执行顺序按照任务的提交顺序执行。
2. newFixedThreadPool
创建固定大小的线程池。每次提交一个任务就创建一个线程,直到线程达到线程池的最大数。线程池的大小一旦达到最大值就会保持不变,如果某个线程因为执行异常而结束,那么线程池会补充一个新线程。
3. newCachedThreadPool
创建一个可缓存的线程池。如果线程池的大小超过了处理任务所需要的线程,那么就会回收部分空闲(貌似60秒不执行任务)的线程,当任务数增加时,此线程池又可以智能的添加新线程来处理任务。此线程池不会对线程池大小做限制,线程池大小完全依赖于操作系统(或者说JVM)能够创建的最大线程大小。
4. newScheduledThreadPool
创建一个大小无限的线程池。此线程池支持定时以及周期性执行任务的需求。
上述的创建方式本质上都创建了ThreadPoolExecutor对象,只是进行了不同的构造参数定制。
实例
1:newSingleThreadExecutor
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;class MyThread extends Thread { @Override public void run() { System.out.println(Thread.currentThread().getName() + " Thread 正在执行。。。"); }}class MyRunnable implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + " Runnable 正在执行。。。"); }}public class Test { public static void main(String[] args) { ExecutorService pool = Executors.newSingleThreadExecutor(); Thread t1 = new MyThread(); Thread t2 = new MyThread(); MyRunnable r1 = new MyRunnable(); MyRunnable r2 = new MyRunnable(); // 永远只有一个线程在执行,无论传入的是Thread还是Runnable,并且保证线程死后自动创建一个新的继续执行任务 pool.execute(t1);//传线程对象是不科学的做法 pool.execute(t2);//传线程对象是不科学的做法 pool.execute(r1);//ok pool.execute(r2);//ok pool.shutdown(); }}输出结果
pool-1-thread-1 Thread 正在执行。。。pool-1-thread-1 Thread 正在执行。。。pool-1-thread-1 Runnable 正在执行。。。pool-1-thread-1 Runnable 正在执行。。。
2 newFixedThreadPool
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;class MyRunnable implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + " Runnable 正在执行。。。"); }}public class Test { public static void main(String[] args) { // 创建一个可重用固定线程数的线程池 ExecutorService pool = Executors.newFixedThreadPool(2); // 创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口 // 将Runnable放入池中进行执行 pool.execute(new MyRunnable()); pool.execute(new MyRunnable()); pool.execute(new MyRunnable()); pool.execute(new MyRunnable()); pool.execute(new MyRunnable()); // 关闭线程池 pool.shutdown(); }}
输出结果
pool-1-thread-2 Runnable 正在执行。。。pool-1-thread-1 Runnable 正在执行。。。pool-1-thread-2 Runnable 正在执行。。。pool-1-thread-1 Runnable 正在执行。。。pool-1-thread-2 Runnable 正在执行。。。可以看到一直都是只有两个线程在工作。
3 newCachedThreadPool
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;class MyRunnable implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + " Runnable 正在执行。。。"); }}public class Test { public static void main(String[] args) { // 创建一个可重用固定线程数的线程池 ExecutorService pool = Executors.newCachedThreadPool(); // 将线程放入池中进行执行 pool.execute(new MyRunnable()); pool.execute(new MyRunnable()); pool.execute(new MyRunnable()); pool.execute(new MyRunnable()); pool.execute(new MyRunnable()); // 关闭线程池 pool.shutdown(); }}输出结果:
pool-1-thread-1 Runnable 正在执行。。。pool-1-thread-1 Runnable 正在执行。。。pool-1-thread-2 Runnable 正在执行。。。pool-1-thread-3 Runnable 正在执行。。。pool-1-thread-4 Runnable 正在执行。。。它的作用是:如果线程池中没有空闲的了就创建新的,有则不创建,直接使用,
他的另一个作用是:线程空闲时间达到一定长度(默认60秒)后会自动回收多余的线程。
newCachedThreadPool非常适合数量多而执行周期短的任务,比如http请求处理任务。
4 newScheduledThreadPool
import java.util.concurrent.*;public class Test { public static void main(String[] args) { ScheduledExecutorService exec = Executors.newScheduledThreadPool(5); exec.scheduleAtFixedRate(new Runnable() {// 每隔一段时间就触发异常 @Override public void run() { //throw new RuntimeException(); System.out.println(Thread.currentThread().getName() + "================"); } }, 100, 3000, TimeUnit.MILLISECONDS); exec.scheduleAtFixedRate(new Runnable() {// 每隔一段时间打印系统时间,证明两者是互不影响的 @Override public void run() { System.out.println(Thread.currentThread().getName() + " Runnable 正在执行。。。"); } }, 100, 1000, TimeUnit.MILLISECONDS); }}
输出:
pool-1-thread-1================pool-1-thread-2 Runnable 正在执行。。。pool-1-thread-1 Runnable 正在执行。。。pool-1-thread-4 Runnable 正在执行。。。pool-1-thread-2================pool-1-thread-3 Runnable 正在执行。。。pool-1-thread-3 Runnable 正在执行。。。pool-1-thread-3 Runnable 正在执行。。。pool-1-thread-1================pool-1-thread-5 Runnable 正在执行。。。pool-1-thread-5 Runnable 正在执行。。。pool-1-thread-5 Runnable 正在执行。。。pool-1-thread-2================pool-1-thread-3 Runnable 正在执行。。。pool-1-thread-3 Runnable 正在执行。。。pool-1-thread-3 Runnable 正在执行。。。pool-1-thread-1================pool-1-thread-5 Runnable 正在执行。。。pool-1-thread-5 Runnable 正在执行。。。pool-1-thread-5 Runnable 正在执行。。。pool-1-thread-2================pool-1-thread-3 Runnable 正在执行。。。pool-1-thread-3 Runnable 正在执行。。。pool-1-thread-3 Runnable 正在执行。。。...可以看出创建了一个定时执行线程池,大小为5,在周期到的时候随机选择一个线程执行任务,
其功能和Timer一样,但相比Timer,它是多线程执行的,在可以并发的任务环境下可以有更好的性能。
- java 线程池(ThreadPoolExecutor)
- java 线程池(ThreadPoolExecutor)
- Java 线程池ThreadPoolExecutor
- Java 线程池ThreadPoolExecutor
- Java 线程池ThreadPoolExecutor
- JAVA线程池:ThreadPoolExecutor
- JAVA线程池ThreadPoolExecutor
- JAVA线程池ThreadPoolExecutor
- ThreadPoolExecutor java 线程池
- Java 线程池ThreadPoolExecutor
- java 线程池ThreadPoolExecutor
- JAVA线程池ThreadPoolExecutor
- java线程池:ThreadPoolExecutor
- Java线程池ThreadPoolExecutor
- Java 线程池 ThreadPoolExecutor
- java线程池ThreadPoolExecutor
- Java线程池ThreadPoolExecutor
- java 线程池ThreadPoolExecutor
- Android获取手机屏幕分辨率
- matlab 函数零散记录
- iOS学习之UINavigationController详解与使用(一)
- 让VisualSVN可以在客户端修改密码
- 提高工作效率的8个小方法
- java 线程池(ThreadPoolExecutor)
- 解决fatal: unable to connect to github.com问题
- PyQt4 精彩实例分析* 实例5 各种消息框的使用
- Java集合框架之小结
- 读excel文件
- 【数据结构与算法】——排序算法篇
- iOS学习之UINavigationController详解与使用(二)
- SQL学习----添加列到指定位置
- Google C++ 编程风格学习