线程池的简单介绍

来源:互联网 发布:哪个银行的淘宝卡最好 编辑:程序博客网 时间:2024/04/28 14:02

Java通过Executors提供四种线程池,分别为:
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

 

(1) newCachedThreadPool
创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。示例代码如下:

Java代码  收藏代码
  1. package test;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4. public class ThreadPoolExecutorTest {  
  5.  public static void main(String[] args) {  
  6.   ExecutorService cachedThreadPool = Executors.newCachedThreadPool();  
  7.   for (int i = 0; i < 10; i++) {  
  8.    final int index = i;  
  9.    try {  
  10.     Thread.sleep(index * 1000);  
  11.    } catch (InterruptedException e) {  
  12.     e.printStackTrace();  
  13.    }  
  14.    cachedThreadPool.execute(new Runnable() {  
  15.     public void run() {  
  16.      System.out.println(index);  
  17. System.out.println("当前线程-->"+Thread.currentThread().getName());
  18.     }  
  19.    });  
  20.   }  
  21.  }  
  22. }  
  23. /*结果:index==0
    当前线程-->pool-1-thread-1
  24. index==1
    当前线程-->pool-1-thread-1
  25. ...*/
  26. //都在线程1中执行

 

线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。
 
(2) newFixedThreadPool
创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。示例代码如下:

Java代码  收藏代码
  1. package test;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4. public class ThreadPoolExecutorTest {  
  5.  public static void main(String[] args) {  
  6.   ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);  
  7.   for (int i = 0; i < 10; i++) {  
  8.    final int index = i;  
  9.    fixedThreadPool.execute(new Runnable() {  
  10.     public void run() {  
  11.      try {  
  12.       System.out.println(index);  
  13. System.out.println("当前线程-->"+Thread.currentThread().getName());
  14.       Thread.sleep(2000);  
  15.      } catch (InterruptedException e) {  
  16.       e.printStackTrace();  
  17.      }  
  18.     }  
  19.    });  
  20.   }  
  21.  }  
  22. }  
  23. /**结果:
  24. index==0
    当前线程-->pool-1-thread-1
    index==1
    当前线程-->pool-1-thread-2
    index==2
    当前线程-->pool-1-thread-3
    index==3
    当前线程-->pool-1-thread-4
    index==4
    当前线程-->pool-1-thread-1
    index==5
    当前线程-->pool-1-thread-2
    index==6
    当前线程-->pool-1-thread-3
    index==7
    当前线程-->pool-1-thread-4
    index==8
    当前线程-->pool-1-thread-1
    index==9
    当前线程-->pool-1-thread-2
  25. */

 
因为线程池大小为3,每个任务输出index后sleep 2秒,所以每两秒打印3个数字。
定长线程池的大小最好根据系统资源进行设置。如Runtime.getRuntime().availableProcessors()

 

(3)  newScheduledThreadPool
创建一个定长线程池,支持定时及周期性任务执行。延迟执行示例代码如下:

Java代码  收藏代码
  1. package test;  
  2. import java.util.concurrent.Executors;  
  3. import java.util.concurrent.ScheduledExecutorService;  
  4. import java.util.concurrent.TimeUnit;  
  5. public class ThreadPoolExecutorTest {  
  6.  public static void main(String[] args) {  
  7.   ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);  
  8.   scheduledThreadPool.schedule(new Runnable() {  
  9.    public void run() {  
  10.     System.out.println("delay 3 seconds");  
  11.    }  
  12.   }, 3, TimeUnit.SECONDS);  
  13.  }  
  14. }  

 
表示延迟3秒执行。

定期执行示例代码如下:

Java代码  收藏代码
  1. package test;  
  2. import java.util.concurrent.Executors;  
  3. import java.util.concurrent.ScheduledExecutorService;  
  4. import java.util.concurrent.TimeUnit;  
  5. public class ThreadPoolExecutorTest {  
  6.  public static void main(String[] args) {  
  7.   ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);  
  8.   scheduledThreadPool.scheduleAtFixedRate(new Runnable() {  
  9.    public void run() {  
  10.     System.out.println("hehe");
  11. System.out.println("当前线程-->"+Thread.currentThread().getName());  
  12.    }  
  13.   }, 13, TimeUnit.SECONDS);  
  14.  }  
  15. }  
  16. /**结果:
  17. hehe
    当前线程-->pool-1-thread-1
    hehe
    当前线程-->pool-1-thread-1
    hehe
    当前线程-->pool-1-thread-2
    hehe
    当前线程-->pool-1-thread-2
    hehe
    当前线程-->pool-1-thread-3
    hehe
    当前线程-->pool-1-thread-1
    hehe
    当前线程-->pool-1-thread-1
  18. */
  19. //运行的线程随机

 
表示延迟1秒后每3秒执行一次。

 

(4) newSingleThreadExecutor
创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。示例代码如下:

Java代码  收藏代码
  1. package test;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4. public class ThreadPoolExecutorTest {  
  5.  public static void main(String[] args) {  
  6.   ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();  
  7.   for (int i = 0; i < 10; i++) {  
  8.    final int index = i;  
  9.    singleThreadExecutor.execute(new Runnable() {  
  10.     public void run() {  
  11.      try {  
  12.       System.out.println(index);  
  13.       Thread.sleep(2000);  
  14.      } catch (InterruptedException e) {  
  15.       e.printStackTrace();  
  16.      }  
  17.     }  
  18.    });  
  19.   }  
  20.  }  
  21. }  



0 0