生命周期:
线程池:预先创建线程的一种技术。线程池在还没有任务到来之前,创建一定数量的线程,放入空闲队列中,然后对这些资源进行复用,减少频繁的创建和销毁对象。线程池保留的是线程对象。
jdk1.5版本以上提供了现成的线程池。、
Java里面线程池的顶级接口是Executor,是一个线程执行的工具。
线程池接口是ExecutorService。根据Executor来创建一个ExecutorService服务。
为什么要这么做呢?
因为在代码中创建线程看似很轻松,但是创建线程其实要消耗一定性能的,底层的消耗可能要比创建一个对象消耗的大。如果我们创建的线程只执行了一个简单的任务就结束了的话,该线程就浪费了。
上面两个都是接口,我们实际应用中会更多的应用Executors类
创建线程池的四种方法:
在Executors类里面提供了一些静态工厂,生成一些常用的线程池
1.newSingleThreadExecutor:
创建一个单线程的线程池。这个线程池只有一个线程在工作,也就是相当于单线程串执行所有任务。如果这个唯一的线程因为异常结束,那么会有一个新的线程来替代它。线程池保证所有任务的执行顺序按照任务的提交顺序执行。虽然只有一个线程,但是可以重复利用。
package com.chocus.demo1;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadDemo8 {
public static void main(String[] args) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Chuier chuier = new Chuier();
executorService.execute(chuier);
executorService.execute(chuier);
executorService.shutdown();
}
}
class Chuier implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "-->" + i);
}
}
}
结果:单线程池只有一个线程,即使有多个任务页串行执行任务。
pool-1-thread-1-->0
pool-1-thread-1-->1
pool-1-thread-1-->2
pool-1-thread-1-->3
pool-1-thread-1-->4
pool-1-thread-1-->5
pool-1-thread-1-->6
pool-1-thread-1-->7
pool-1-thread-1-->8
pool-1-thread-1-->9
pool-1-thread-1-->0
pool-1-thread-1-->1
pool-1-thread-1-->2
pool-1-thread-1-->3
pool-1-thread-1-->4
pool-1-thread-1-->5
pool-1-thread-1-->6
pool-1-thread-1-->7
pool-1-thread-1-->8
pool-1-thread-1-->9
2.newFixedThreadPool:
创建固定大小的线程池。每次提交一个任务就创建一个线程,直到线程达到线程池的最大大小。线程池的大小一旦达到最大值就会保持不变。如果某个线程因为执行异常而结束。那么线程池会补充一个新线程。
public class ThreadDemo8 {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
Chuier chuier = new Chuier();
executorService.execute(chuier);
executorService.execute(chuier);
executorService.shutdown();
}
}
结果:固定两个连接的线程池,一起执行任务
pool-1-thread-2-->0
pool-1-thread-1-->0
pool-1-thread-2-->1
pool-1-thread-1-->1
pool-1-thread-2-->2
pool-1-thread-1-->2
pool-1-thread-2-->3
pool-1-thread-1-->3
pool-1-thread-1-->4
pool-1-thread-2-->4
pool-1-thread-2-->5
pool-1-thread-1-->5
pool-1-thread-2-->6
pool-1-thread-1-->6
pool-1-thread-2-->7
pool-1-thread-1-->7
pool-1-thread-2-->8
pool-1-thread-1-->8
pool-1-thread-1-->9
pool-1-thread-2-->9
固定一个连接的线程池,串行执行任务
public class ThreadDemo8 {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(1);
Chuier chuier = new Chuier();
executorService.execute(chuier);
executorService.execute(chuier);
executorService.shutdown();
}
}
结果:
pool-1-thread-1-->0
pool-1-thread-1-->1
pool-1-thread-1-->2
pool-1-thread-1-->3
pool-1-thread-1-->4
pool-1-thread-1-->5
pool-1-thread-1-->6
pool-1-thread-1-->7
pool-1-thread-1-->8
pool-1-thread-1-->9
pool-1-thread-1-->0
pool-1-thread-1-->1
pool-1-thread-1-->2
pool-1-thread-1-->3
pool-1-thread-1-->4
pool-1-thread-1-->5
pool-1-thread-1-->6
pool-1-thread-1-->7
pool-1-thread-1-->8
pool-1-thread-1-->9
3.newCachedThreadPool:(用的比较少,因为会创建很多线程)
创建一个可缓存的线程池。如果线程池的大小超过了处理任务所需要的线程,那么就会回收部分空闲(60秒不执行任务)的线程,当任务数增加时,此线程池又可以智能的添加新线程来处理任务。此线程池不会对线程池大小做限制,线程池大小完全依赖于操作系统(或者说JVM)能够创建的最大线程大小。
public class ThreadDemo8 {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
Chuier chuier = new Chuier();
executorService.execute(chuier);
executorService.execute(chuier);
executorService.execute(chuier);
executorService.shutdown();
}
}
结果:有几个任务就创建几个线程
pool-1-thread-2-->0
pool-1-thread-1-->0
pool-1-thread-3-->0
pool-1-thread-1-->1
pool-1-thread-2-->1
pool-1-thread-3-->1
pool-1-thread-1-->2
pool-1-thread-2-->2
pool-1-thread-3-->2
pool-1-thread-1-->3
pool-1-thread-3-->3
pool-1-thread-2-->3
pool-1-thread-1-->4
pool-1-thread-3-->4
pool-1-thread-2-->4
pool-1-thread-1-->5
pool-1-thread-3-->5
pool-1-thread-2-->5
pool-1-thread-1-->6
pool-1-thread-3-->6
pool-1-thread-2-->6
pool-1-thread-1-->7
pool-1-thread-3-->7
pool-1-thread-2-->7
pool-1-thread-1-->8
pool-1-thread-3-->8
pool-1-thread-2-->8
pool-1-thread-1-->9
pool-1-thread-3-->9
pool-1-thread-2-->9
4.newScheduledThreadPool:
创建一个大小无限的线程池。此线程池支持定时以及周期性执行任务的需求。
public class ThreadDemo8 {
public static void main(String[] args) {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
Chuier chuier = new Chuier();
executorService.schedule(chuier, 3000, TimeUnit.MILLISECONDS);
executorService.schedule(chuier, 2000, TimeUnit.MILLISECONDS);
executorService.schedule(chuier, 1000, TimeUnit.MILLISECONDS);
executorService.shutdown();
}
}
结果:有几个任务创建几个线程,延迟到指定时间单位后开始执行
pool-1-thread-1-->0
pool-1-thread-1-->1
pool-1-thread-1-->2
pool-1-thread-1-->3
pool-1-thread-3-->0
pool-1-thread-1-->4
pool-1-thread-3-->1
pool-1-thread-1-->5
pool-1-thread-3-->2
pool-1-thread-1-->6
pool-1-thread-3-->3
pool-1-thread-2-->0
pool-1-thread-1-->7
pool-1-thread-3-->4
pool-1-thread-2-->1
pool-1-thread-1-->8
pool-1-thread-3-->5
pool-1-thread-2-->2
pool-1-thread-1-->9
pool-1-thread-3-->6
pool-1-thread-2-->3
pool-1-thread-3-->7
pool-1-thread-2-->4
pool-1-thread-3-->8
pool-1-thread-2-->5
pool-1-thread-3-->9
pool-1-thread-2-->6
pool-1-thread-2-->7
pool-1-thread-2-->8
pool-1-thread-2-->9