创建新线程的服务器在创建和销毁线程上花费的时间和消耗的系统资源要比花在处理实际的用户请求的时间和资源更多,线程池为线程生命周期开销问题和资源不足问题提供了解决方案。通过对多个任务重用线程,线程创建的开销被分摊到了多个任务上。 其好处是,因为在请求到达时线程已经存在,所以无意中也消除了线程创建所带来的 延迟。这样,就可以立即为请求服务,使应用程序响应更快。而且,通过适当地调整 线程池中的线程数目,也就是当请求的数目超过某个阈值时,就强制其它任何新到的请求一直等待,直到获得一个线程来处理为止,从而可以防止资源不足。
示例代码:
ThreadPoolTest类:
- public class ThreadPoolTest {
- //单一线程数:就是只有一个线程开启,如果多长执行的话就是,这个线程一直轮询执行下去
- @SuppressWarnings(“unused”)
- private ExecutorService executorService1 = Executors.newSingleThreadExecutor();
- //固定线程数:在程序初始化的时候,虚拟机初始化5个线程
- @SuppressWarnings(“unused”)
- private ExecutorService executorService2 = Executors.newFixedThreadPool(5);
- //缓存线程数:在程序初始化的时候创建5线程线程,并随着系统的需求增加线程数量,线程数量的上限是系统资源的上线
- private ExecutorService executorService3 = Executors.newScheduledThreadPool(5);
- private static int num = 50;
- public void threadPool() throws InterruptedException {
- //创建了50个线程
- for(int i = 0; i < 50; i ++) {
- executorService3.execute( new Runnable() {
- public void run() {
- try {
- Thread.sleep( new Random().nextInt(1700));
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finish();
- System.out.println(Thread.currentThread().getName() + “当前剩余任务数量:” + (num));
- }
- });
- }
- }
- public synchronized void finish() {
- num–;
- if(num == 0) {
- System.out.println( “线程池所有任务执行完毕”);
- }
- }
- }
Main类:
- public class Main {
- public static void main(String[] args) throws Exception {
- System.out.println( “———–threadPool测试—————–“);
- new ThreadPoolTest().threadPool();
- }
测试结果:
可以看到5个线程在执行50个任务,只要线程池里有空闲线程,就会使该线程执行任务。
另外,还可以通过实现Runnable接口,使用线程池ExecutorService的submit方法来向线程池提交任务。
示例代码:
ThreadPoolSubmit类:
- public class ThreadPoolSubmit implements Runnable{
- private static int num = 50;
- public void run() {
- try {
- Thread.sleep( new Random().nextInt(1700));
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finish();
- System.out.println(Thread.currentThread().getName() + “当前剩余任务数量:” + (num));
- }
- public synchronized void finish() {
- num–;
- if(num == 0) {
- System.out.println( “线程池所有任务执行完毕”);
- }
- }
- public static void main(String[] args) {
- ExecutorService executorService = Executors.newScheduledThreadPool( 5);
- //50个任务
- for (int i = 0; i < 50; i++) {
- executorService.submit( new ThreadPoolSubmit());
- }
- }
- }
测试结果与上例相同。