Java四种线程池详解newFixedThreadPool、newCachedThreadPool、newScheduledThreadPool、newSingleThreadExecutor
1.newFixedThreadPool:固定长度的线程池,每提交一个任务就创建一个线程,直到达到线程池的最大数量,这时线程规模将不再变化。
1 import java.util.concurrent.ExecutorService; 2 import java.util.concurrent.Executors; 3 4 public class FixedThreadPoolPrac { 5 public static void main(String[] args) { 6 ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3); 7 for (int i = 0; i < 10; i++) { 8 final int index = i; 9 fixedThreadPool.execute(new Runnable() { 10 11 public void run() { 12 try { 13 System.out.print("当前线程"+index); 14 System.out.print("当前活跃线程池大小:"+Thread.activeCount()); 15 System.out.println(); 16 Thread.sleep(2000); 17 } catch (InterruptedException e) { 18 // TODO Auto-generated catch blocke.printStackTrace(); 19 } 20 } 21 }); 22 } 23 fixedThreadPool.shutdown(); 24 } 25 }
2.newCachedThreadPool:一个可缓存的线程池,线程池的规模超过了处理需求,将自动回收空闲线程,当需求增加时,可以添加新线程,线程池的规模不存在任何限制,没有顺序执行。
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CachedThreadPoolPrac { public static void main(String[] args){ ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for(int i=0 ; i<10; i++){ final int index = i; try { Thread.sleep(index*1000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } cachedThreadPool.execute(new Runnable(){ public void run() { try{ System.out.print("当前线程"+index); System.out.print("当前活跃线程池大小:"+Thread.activeCount()); System.out.println(); }catch (Exception e) { // TODO: handle exception } } }); } cachedThreadPool.shutdown(); } }
3.newScheduledThreadPool :创建一个固定长度的线程池,而且以延迟或定时的方式来执行任务。创建一个定长线程池,支持定时及周期性任务执行。
-
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class ScheduledThreadPoolPrac { public static void main(String[] args) { final ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1); /*scheduledThreadPool.schedule(new Runnable() { public void run() { System.out.println("delay 3 seconds"); } }, 3, TimeUnit.SECONDS); }*/ scheduledThreadPool.scheduleAtFixedRate(new Runnable() { int i = 0; public void run() { System.out.println("delay 1 seconds, and excute every 3 seconds"); System.out.println("当前活跃线程池"+Thread.activeCount()); if(i>5){ scheduledThreadPool.shutdown(); }else{ i++; } } }, 1, 3, TimeUnit.SECONDS);} }
4.newSingleThreadExecutor:单个工作线程来执行任务,如果这个线程异常结束,就会有一个新的来替代它,它的特点是能确保任务在队列中的顺序来串行执行。
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SingleThreadPoolPrac { public static void main(String[] args) { ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10; i++) { final int index = i; singleThreadExecutor.execute(new Runnable() { public void run() { try { System.out.println(index); Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } singleThreadExecutor.shutdown(); } }
喜欢请赞赏一下啦^_^
微信赞赏
![](https://images.cnblogs.com/cnblogs_com/ljf-Sky/1807277/o_200716012544%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20200716092510.jpg)
支付宝赞赏
![](https://images.cnblogs.com/cnblogs_com/ljf-Sky/1807277/o_200716012531%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20200716092459.jpg)