java 线程池(ThreadPoolExecutor)
来源:互联网 发布:windows loader2.2.1 编辑:程序博客网 时间:2024/05/16 12:48
在Executors类里面提供了一些静态工厂,生成一些常用的线程池。
- newSingleThreadExecutor
创建一个单线程的线程池。这个线程池只有一个线程在工作,如果这个唯一的线程因为异常结束,那么会有一个新的线程来替代它。此线程池保证所有任务的执行顺序按照任务的提交顺序执行。
- newFixedThreadPool
创建固定大小的线程池。每次提交一个任务就创建一个线程,直到线程达到线程池的最大数。线程池的大小一旦达到最大值就会保持不变,如果某个线程因为执行异常而结束,那么线程池会补充一个新线程。
- newCachedThreadPool
创建一个可缓存的线程池。如果线程池的大小超过了处理任务所需要的线程,那么就会回收部分空闲(貌似60秒不执行任务)的线程,当任务数增加时,此线程池又可以智能的添加新线程来处理任务。此线程池不会对线程池大小做限制,线程池大小完全依赖于操作系统(或者说JVM)能够创建的最大线程大小。
- newScheduledThreadPool
创建一个大小无限的线程池。此线程池支持定时以及周期性执行任务的需求。
上述的创建方式本质上都创建了ThreadPoolExecutor对象,只是进行了不同的构造参数定制。
实例
1:newSingleThreadExecutor
[java] view plain copy
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(); } }
输出结果
[java] view plain copy
pool-1-thread-1 Thread 正在执行。。。 pool-1-thread-1 Thread 正在执行。。。 pool-1-thread-1 Runnable 正在执行。。。 pool-1-thread-1 Runnable 正在执行。。。
2 newFixedThreadPool
[java] view plain copy
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(); } }
输出结果
[java] view plain copy
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
[java] view plain copy
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(); } }
输出结果:
[java] view plain copy
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
[java] view plain copy
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); } }
输出:
[java] view plain copy
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
- java正则表达式去除html中所有的标签和特殊HTML字符
- Android学习之如何同时更新不同界面的UI
- 音视频实时传输错误掩盖算法
- ELK日志平台搭建logstash+elasticsearch+redis+Kibana
- 2005-Unknown MySQL server host 'localhost' 错误解决办法
- java 线程池(ThreadPoolExecutor)
- 物体检测系列之SSD原理介绍
- Cadence PCB update DRC 时卡住,程序崩溃的解决方法
- matable的作用
- 左侧导航菜单 子菜单的显示隐藏效果
- Android_Adapter,Spanner,listView及其优化
- PHP 上传文件报错处理
- hnust
- c++学习-reference to non-static member function must be called