ThreadPoolDemo
1 package com.miaoshaProject.demo; 2 3 import java.util.concurrent.ExecutorService; 4 import java.util.concurrent.Executors; 5 import java.util.concurrent.ScheduledExecutorService; 6 import java.util.concurrent.TimeUnit; 7 8 /** 9 * @Author wangshuo 10 * @Date 2022/5/12, 12:55 11 * 为什么要使用线程池: 12 * 诸如 Web 服务器、数据库服务器、文件服务器或邮件服务器之类的许多服务器应用程序都面向处理来自某些远程来源的大量短小的任务。 13 * 请求以某种方式到达服务器,这种方式可能是通过网络协议(例如 HTTP、FTP 或 POP)、通过 JMS 队列或者可能通过轮询数据库。 14 * 不管请求如何到达,服务器应用程序中经常出现的情况是:单个任务处理的时间很短而请求的数目却是巨大的。 15 * 构建服务器应用程序的一个简单模型是:每当一个请求到达就创建一个新线程,然后在新线程中为请求服务。 16 * 实际上对于原型开发这种方法工作得很好,但如果试图部署以这种方式运行的服务器应用程序,那么这种方法的严重不足就很明显。 17 * 每个请求对应一个线程(thread-per-request)方法的不足之一是:为每个请求创建一个新线程的开销很大; 18 * 为每个请求创建新线程的服务器在创建和销毁线程上花费的时间和消耗的系统资源要比花在处理实际的用户请求的时间和资源更多。 19 * 除了创建和销毁线程的开销之外,活动的线程也消耗系统资源。 20 * 在一个 JVM 里创建太多的线程可能会导致系统由于过度消耗内存而用完内存或“切换过度”。 21 * 为了防止资源不足,服务器应用程序需要一些办法来限制任何给定时刻处理的请求数目。 22 * 线程池为线程生命周期开销问题和资源不足问题提供了解决方案。 23 * 通过对多个任务重用线程,线程创建的开销被分摊到了多个任务上。 24 * 其好处是,因为在请求到达时线程已经存在,所以无意中也消除了线程创建所带来的延迟。 25 * 这样,就可以立即为请求服务,使应用程序响应更快。 26 * 而且,通过适当地调整线程池中的线程数目,也就是当请求的数目超过某个阈值时,就强制其它任何新到的请求一直等待, 27 * 直到获得一个线程来处理为止,从而可以防止资源不足。 28 * <p> 29 * <p> 30 * 常用的几种线程池demo 31 */ 32 public class ThreadPoolDemo { 33 34 //newCachedThreadPool 可缓存线程池,可灵活回收空闲线程 35 /* 36 特点:工作线程的创建数量几乎没有限制(其实也有限制的,数目为Integer.MAX_VALUE), 这样可灵活的往线程池中添加线程。 37 如果长时间没有往线程池中提交任务,即如果工作线程空闲了指定的时间(默认为1分钟),则该工作线程将自动终止。 38 终止后,如果你又提交了新的任务,则线程池重新创建一个工作线程。 39 40 在使用CachedThreadPool时,一定要注意控制任务的数量,否则,由于大量线程同时运行,很有会造成系统瘫痪。 41 */ 42 public static class newCachedClass { 43 44 public static void main(String[] args) { 45 ExecutorService newCachedThreadPool = Executors.newCachedThreadPool(); 46 for (int i = 0; i < 10; i++) { 47 final int index = i; 48 try { 49 Thread.sleep(index * 1000); 50 } catch (InterruptedException e) { 51 e.printStackTrace(); 52 } 53 newCachedThreadPool.execute(() -> { 54 System.out.println(index); 55 }); 56 } 57 } 58 } 59 60 //newFixedThreadPool 创建一个指定工作线程数量的线程池。 61 /* 62 特点:每当提交一个任务就创建一个工作线程,如果工作线程数量达到线程池初始的最大数,则将提交的任务存入到池队列中。 63 它具有线程池提高程序效率和节省创建线程时所耗的开销的优点。 64 但是,在线程池空闲时,即线程池中没有可运行任务时,它不会释放工作线程,还会占用一定的系统资源。 65 */ 66 public static class newFixedClass { 67 68 public static void main(String[] args) { 69 ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(3);//初始最大数 70 for (int i = 0; i < 30; i++) { 71 final int index = i; 72 newFixedThreadPool.execute(() -> { 73 try { 74 System.out.println(index); 75 Thread.sleep(2000); 76 } catch (InterruptedException e) { 77 e.printStackTrace(); 78 } 79 }); 80 } 81 } 82 } 83 84 //newSingleThreadExecutor 顺序执行线程池 85 /* 86 特点: 87 创建一个单线程化的Executor,即只创建唯一的工作者线程来执行任务。 88 它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO,优先级)执行。 89 如果这个线程异常结束,会有另一个取代它,保证顺序执行。 90 单工作线程最大的特点是可保证顺序地执行各个任务,并且在任意给定的时间不会有多个线程是活动的。 91 */ 92 public static class newSingleClass { 93 94 public static void main(String[] args) { 95 ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor(); 96 for (int i = 0; i < 10; i++) { 97 final int index = i; 98 newSingleThreadExecutor.execute(() -> { 99 try { 100 System.out.println(index); 101 Thread.sleep(2000); 102 } catch (InterruptedException e) { 103 e.printStackTrace(); 104 } 105 }); 106 } 107 } 108 } 109 110 //newScheduledThreadPool 特点:定长的线程池,支持定时任务以及周期性任务执行 111 public static class newScheduleClass { 112 113 public static void main(String[] args) { 114 115 ScheduledExecutorService newScheduledThreadPool = 116 Executors.newScheduledThreadPool(5);//线程池长度 117 //延迟三秒后执行 118 demoSchedule(newScheduledThreadPool); 119 //延迟一秒后,每三秒执行一次 120 demoScheduleAtFixedRate(newScheduledThreadPool); 121 } 122 123 private static void demoSchedule(ScheduledExecutorService newScheduledThreadPool){ 124 125 newScheduledThreadPool.schedule(new Runnable() { 126 @Override 127 public void run() { 128 System.out.println("delay 3 seconds"); 129 } 130 }, 3, TimeUnit.SECONDS); 131 } 132 133 private static void demoScheduleAtFixedRate(ScheduledExecutorService newScheduledThreadPool){ 134 135 newScheduledThreadPool.scheduleAtFixedRate(new Runnable() { 136 @Override 137 public void run() { 138 System.out.println("delay 1 seconds , and execute every 3 seconds"); 139 } 140 }, 1, 3, TimeUnit.SECONDS); 141 } 142 } 143 }
本文来自博客园,作者:荣慕平,转载请注明原文链接:https://www.cnblogs.com/rongmuping/articles/16262244.html