一,线程池:提供了一个线程队列,队列中保存着所有等待状态的线程,避免了创建与销毁额外的开销,提高了响应速度。
二,线程池的体系结构
java.util.concurrent.Exector:负责线程的使用与调度的根接口
|-- ExecutorService 为Exector子接口:线程池等的主要接口
|-- ThreadPoolExecutor 线程池的实现类
|-- ScheduledExecutorService 为ExecutorService的子接口:负责线程的调度
|-- ScheduledThreadPoolExecutor:继承ThreadPoolExecutor和实现了ScheduledExecutorService接口
三,工具类:Executors
1.ExecutorService pool = Executors.newFixedThreadPool(5);创建了含有5个线程的线程池
2.ExecutorService pool = Executors.newCachedThreadExecutor();创建了缓存线程池,线程数量不固定
3.ExecutorService pool = Executors.newSingleThreadExecutor();创建了含有单个线程的线程池
3.ScheduledExecutorService pool = Executors.newScheduledThreadPool();创建固定大小的线程池,可以延迟或者定时的执行任务
代码例子:
1 package xianchengtest; 2 3 4 import java.util.ArrayList; 5 import java.util.List; 6 import java.util.concurrent.Callable; 7 import java.util.concurrent.ExecutorService; 8 import java.util.concurrent.Executors; 9 import java.util.concurrent.Future; 10 11 public class TestThreadPool { 12 13 public static void main(String[] args) throws Exception { 14 //1.创建线程池 15 ExecutorService pool = Executors.newFixedThreadPool(5); 16 //创建一个list集合用于回收结果 17 List<Future<Integer>> list = new ArrayList<>(); 18 for (int i = 0; i < 10; i++) { 19 Future<Integer> future = pool.submit(new Callable<Integer>(){ 20 21 @Override 22 public Integer call() throws Exception { 23 int sum = 0; 24 for (int i = 0; i <= 100; i++) { 25 sum += i ; 26 } 27 return sum; 28 } 29 }); 30 list.add(future);//加入回收结果 31 } 32 //关闭线程池 33 pool.shutdown(); 34 35 for (Future<Integer> future : list) { 36 System.out.println(future.get()); 37 } 38 39 //另外一种使用线程池的方法 40 // ThreadPoolDemo th = new ThreadPoolDemo(); 41 // //2.为线程池中的线程分配任务 42 // for (int i = 0; i < 10; i++) { 43 // pool.submit(th); 44 // } 45 // //3.关闭线程池 46 // pool.shutdown(); 47 } 48 //以前的启动线程的方法,要一个个的启动 49 // new Thread(th).start(); 50 // new Thread(th).start(); 51 } 52 class ThreadPoolDemo implements Runnable{ 53 private int i = 0; 54 55 @Override 56 public void run() { 57 while(i<=100) { 58 System.out.println(Thread.currentThread().getName() + " : "+ i++); 59 } 60 } 61 62 }
带延时执行的线程池代码例子:
1 package xianchengtest; 2 3 import java.util.Random; 4 import java.util.concurrent.Callable; 5 import java.util.concurrent.ExecutionException; 6 import java.util.concurrent.Executors; 7 import java.util.concurrent.Future; 8 import java.util.concurrent.ScheduledExecutorService; 9 import java.util.concurrent.TimeUnit; 10 11 public class TestScheduledThreadPool { 12 13 public static void main(String[] args) throws Exception { 14 //1.创建线程池 15 ScheduledExecutorService pool = Executors.newScheduledThreadPool(5); 16 //2.分配任务 17 18 for (int i = 0; i < 10; i++) { 19 Future<Integer> future = pool.schedule(new Callable<Integer>() { 20 21 @Override 22 public Integer call() throws Exception { 23 int num = new Random().nextInt(100); 24 System.out.println(Thread.currentThread().getName() + " : "+ num); 25 return num; 26 } 27 },1,TimeUnit.SECONDS); 28 System.out.println(future.get()); 29 } 30 //3.关闭线程池 31 pool.shutdown(); 32 33 } 34 35 }