Java通过Executors提供四种线程池,分别为:
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
线程池的作用:
线程池作用就是限制系统中执行线程的数量。
根 据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果;少了浪费了系统资源,多了造成系统拥挤效率不高。用线程池控制线程数量,其他线程排 队等候。一个任务执行完毕,再从队列的中取最前面的任务开始执行。若队列中没有等待进程,线程池的这一资源处于等待。当一个新任务需要运行时,如果线程池 中有等待的工作线程,就可以开始运行了;否则进入等待队列。
为什么要用线程池:
1.减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务。
2.可以根据系统的承受能力,调整线程池中工作线线程的数目,防止因为消耗过多的内存,而把服务器累趴下(每个线程需要大约1MB内存,线程开的越多,消耗的内存也就越大,最后死机)。
Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具。真正的线程池接口是ExecutorService。
比较重要的几个类:
ExecutorService: 真正的线程池接口。
ScheduledExecutorService: 能和Timer/TimerTask类似,解决那些需要任务重复执行的问题。
ThreadPoolExecutor: ExecutorService的默认实现。
ScheduledThreadPoolExecutor: 继承ThreadPoolExecutor的ScheduledExecutorService接口实现,周期性任务调度的类实现。
要配置一个线程池是比较复杂的,尤其是对于线程池的原理不是很清楚的情况下,很有可能配置的线程池不是较优的,因此在Executors类里面提供了一些静态工厂,生成一些常用的线程池。
newCachedThreadPool
- public static void main(String[] args) {
- ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
- for (int i = 0; i < 10; i++) {
- final int index = i;
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- cachedThreadPool.execute(new Runnable() {
- public void run() {
- System.out.println(index);
- }
- });
- }
- }
newFixedThreadPool
- public static void main(String[] args) {
- ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
- for (int i = 0; i < 10; i++) {
- final int index = i;
- fixedThreadPool.execute(new Runnable() {
- public void run() {
- try {
- System.out.println(index);
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
定长线程池的大小最好根据系统资源进行设置。如Runtime.getRuntime().availableProcessors()
newScheduleThreadPool()
- public static void main(String[] args) {
- ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
- for (int i = 0; i < 10; i++) {
- scheduledThreadPool.schedule(new Runnable() {
- public void run() {
- System.out.println("delay 3 seconds");
- }
- }, 3, TimeUnit.SECONDS);
- }
- }
newSingleThreadExecutor
按顺序执行线程,某个时间段只能有一个线程存在,一个线程死掉,另一个线程会补上
- public static void main(String[] args) {
- ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
- for (int i = 0; i < 10; i++) {
- final int index = i;
- singleThreadExecutor.execute(new Runnable() {
- public void run() {
- /* System.out.println(index);*/
- try {
- System.out.println(index);
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
package thread.demo.test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; class MyThread extends Thread{ public void run(){ System.out.println(Thread.currentThread().getName()+"is running"); } } //class MyThread1 implements Runnable{ // public void run(){ // System.out.println("===="); // } //} public class ThreadPoolDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // ExecutorService pool=Executors.newFixedThreadPool(3); Thread t1=new MyThread(); Thread t2=new MyThread(); Thread t3=new MyThread(); Thread t4=new MyThread(); Thread t5=new MyThread(); // pool.execute(t1); // pool.execute(t2); // pool.execute(t3); // pool.execute(t4); // pool.execute(t5); // pool.shutdown(); /* * output: * pool-1-thread-2is running * pool-1-thread-3is running * pool-1-thread-1is running * pool-1-thread-3is running * pool-1-thread-2is running * */ // ExecutorService pool1=Executors.newCachedThreadPool(); // pool1.execute(t1); // pool1.execute(t2); // pool1.execute(t3); // pool1.execute(t4); // pool1.execute(t5); // pool1.shutdown(); /* * output: * pool-2-thread-2is running * pool-2-thread-3is running * pool-2-thread-1is running * pool-2-thread-4is running * pool-2-thread-5is running */ // ExecutorService pool2=Executors.newSingleThreadExecutor(); // pool2.execute(t1); // pool2.execute(t2); // pool2.execute(t3); // pool2.execute(t4); // pool2.execute(t5); // pool2.shutdown(); /* * OutPut: * pool-3-thread-1is running * pool-3-thread-1is running * pool-3-thread-1is running * pool-3-thread-1is running * pool-3-thread-1is running */ ExecutorService pool3=Executors.newScheduledThreadPool(1); pool3.execute(new Runnable(){ public void run(){ System.out.println(Thread.currentThread().getName()+"===="); } }); pool3.execute(new Runnable(){ public void run(){ System.out.println(Thread.currentThread().getName()+"~~~~"); } }); pool3.shutdown(); /* * output: * pool-3-thread-1==== * pool-3-thread-1~~~~ */ } }