1. 概念
线程池主要是通过Executors这个类来创建 返回的是ExecutorService对象
2.固定大小线程池
例子:创建固定线程数目为3的线程池
package org.lkl.thead.foo.threadpool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * * Function : 线程池 * @author : Liaokailin * CreateDate : 2014-6-18 * version : 1.0 */ public class ExecutorThreadPool { public static void main(String[] args) { /** * 创建固定大小的线程池 */ ExecutorService threadPool = Executors.newFixedThreadPool(3) ; for(int i=1 ;i<=10 ;i++){ final int taskid = i ; threadPool.execute(new Runnable() { @Override public void run() { for(int j=1 ;j<=2 ;j++){ try { Thread.sleep(20) ; } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("["+Thread.currentThread().getName()+" ]is loop of "+j+" for task of "+taskid); } } }) ; } } }
运行结果如下:
[pool-1-thread-1 ]is loop of 1 for task of 1 [pool-1-thread-3 ]is loop of 1 for task of 3 [pool-1-thread-2 ]is loop of 1 for task of 2 [pool-1-thread-2 ]is loop of 2 for task of 2 [pool-1-thread-1 ]is loop of 2 for task of 1 [pool-1-thread-3 ]is loop of 2 for task of 3 [pool-1-thread-2 ]is loop of 1 for task of 4 [pool-1-thread-1 ]is loop of 1 for task of 5 [pool-1-thread-3 ]is loop of 1 for task of 6 [pool-1-thread-2 ]is loop of 2 for task of 4 [pool-1-thread-1 ]is loop of 2 for task of 5 [pool-1-thread-3 ]is loop of 2 for task of 6 [pool-1-thread-1 ]is loop of 1 for task of 7 [pool-1-thread-2 ]is loop of 1 for task of 8 [pool-1-thread-3 ]is loop of 1 for task of 9 [pool-1-thread-1 ]is loop of 2 for task of 7 [pool-1-thread-2 ]is loop of 2 for task of 8 [pool-1-thread-3 ]is loop of 2 for task of 9 [pool-1-thread-1 ]is loop of 1 for task of 10 [pool-1-thread-1 ]is loop of 2 for task of 10
固定的线程池数目为3,那么始终只会有三个线程在运行,只有等到该三个线程的任务完成以后才会去执行其他的任务
3. 缓冲池
ExecutorService threadPool = Executors.newCachedThreadPool() ;
运行结果:
[pool-1-thread-2 ]is loop of 1 for task of 2 [pool-1-thread-1 ]is loop of 1 for task of 1 [pool-1-thread-3 ]is loop of 1 for task of 3 [pool-1-thread-5 ]is loop of 1 for task of 5 [pool-1-thread-4 ]is loop of 1 for task of 4 [pool-1-thread-7 ]is loop of 1 for task of 7 [pool-1-thread-6 ]is loop of 1 for task of 6 [pool-1-thread-10 ]is loop of 1 for task of 10 [pool-1-thread-8 ]is loop of 1 for task of 8 [pool-1-thread-9 ]is loop of 1 for task of 9 [pool-1-thread-1 ]is loop of 2 for task of 1 [pool-1-thread-2 ]is loop of 2 for task of 2 [pool-1-thread-3 ]is loop of 2 for task of 3 [pool-1-thread-5 ]is loop of 2 for task of 5 [pool-1-thread-4 ]is loop of 2 for task of 4 [pool-1-thread-7 ]is loop of 2 for task of 7 [pool-1-thread-6 ]is loop of 2 for task of 6 [pool-1-thread-9 ]is loop of 2 for task of 9 [pool-1-thread-10 ]is loop of 2 for task of 10 [pool-1-thread-8 ]is loop of 2 for task of 8
缓冲池会按照jvm的调度来分配若干个线程来执行当前需要完成的任务 ,上面的例子有是个线程任务要执行,那么自动分配了10个线程来完成
4.单一的线程池
ExecutorService threadPool = Executors.newSingleThreadExecutor() ;
运行结果:
[pool-1-thread-1 ]is loop of 1 for task of 1 [pool-1-thread-1 ]is loop of 2 for task of 1 [pool-1-thread-1 ]is loop of 1 for task of 2 [pool-1-thread-1 ]is loop of 2 for task of 2 [pool-1-thread-1 ]is loop of 1 for task of 3 [pool-1-thread-1 ]is loop of 2 for task of 3 [pool-1-thread-1 ]is loop of 1 for task of 4 [pool-1-thread-1 ]is loop of 2 for task of 4 [pool-1-thread-1 ]is loop of 1 for task of 5 [pool-1-thread-1 ]is loop of 2 for task of 5 [pool-1-thread-1 ]is loop of 1 for task of 6 [pool-1-thread-1 ]is loop of 2 for task of 6 [pool-1-thread-1 ]is loop of 1 for task of 7 [pool-1-thread-1 ]is loop of 2 for task of 7 [pool-1-thread-1 ]is loop of 1 for task of 8 [pool-1-thread-1 ]is loop of 2 for task of 8 [pool-1-thread-1 ]is loop of 1 for task of 9 [pool-1-thread-1 ]is loop of 2 for task of 9 [pool-1-thread-1 ]is loop of 1 for task of 10 [pool-1-thread-1 ]is loop of 2 for task of 10
只会有一个线程来执行当前的任务
5. shutdown 和shutdownNow
ExecutorService对象中有两个方法 一个是shutdown 一个是shutdownNow 看如下代码:
package org.lkl.thead.foo.threadpool; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * * Function : 线程池 * @author : Liaokailin * CreateDate : 2014-6-18 * version : 1.0 */ public class ExecutorThreadPool { public static void main(String[] args) { ExecutorService threadPool = Executors.newFixedThreadPool(3) ; for(int i=1 ;i<=10 ;i++){ final int taskid = i ; threadPool.execute(new Runnable() { @Override public void run() { for(int j=1 ;j<=2 ;j++){ try { // Thread.sleep(20) ; } catch (Exception e) { e.printStackTrace(); } System.out.println("["+Thread.currentThread().getName()+" ]is loop of "+j+" for task of "+taskid); } } }) ; } List<Runnable> list = threadPool.shutdownNow() ; System.out.println("-----"); for(Runnable r :list){ //System.out.println(r.run()); r.run() ; } System.out.println("-------------"); } }
执行的结果:
[pool-1-thread-2 ]is loop of 1 for task of 2 [pool-1-thread-1 ]is loop of 1 for task of 1 [pool-1-thread-1 ]is loop of 2 for task of 1 [pool-1-thread-2 ]is loop of 2 for task of 2 ----- [pool-1-thread-3 ]is loop of 1 for task of 3 [pool-1-thread-3 ]is loop of 2 for task of 3 [main ]is loop of 1 for task of 4 [main ]is loop of 2 for task of 4 [main ]is loop of 1 for task of 5 [main ]is loop of 2 for task of 5 [main ]is loop of 1 for task of 6 [main ]is loop of 2 for task of 6 [main ]is loop of 1 for task of 7 [main ]is loop of 2 for task of 7 [main ]is loop of 1 for task of 8 [main ]is loop of 2 for task of 8 [main ]is loop of 1 for task of 9 [main ]is loop of 2 for task of 9 [main ]is loop of 1 for task of 10 [main ]is loop of 2 for task of 10 -------------
threadPool.shutdown() 表示结束当前的线程池 但是在线程池中的任务还会允许继续运行完成,但是不会接收新的任务,方法返回void
threadPool.shutdownNow() 表示立即结束当前的线程池,不管线程池中是否还存在没有完成的任务,返回一个List<Runnable> 表示线程池中没有运行完的任务
6. 通过线程池启动定时器
ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(3) ; //两秒后执行 scheduledService.schedule(new Runnable() { @Override public void run() { System.out.println("hello"); } }, 2, TimeUnit.SECONDS) ; //两秒后执行 以后每隔3秒执行一次 scheduledService.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("world"); } }, 2, 3, TimeUnit.SECONDS) ;
时间: 2024-10-12 12:19:28