热衷学习,热衷生活!😄
沉淀、分享、成长,让自己和他人都能有所收获!😄
一、newCachedThreadPool的方式
这种方式创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程用于执行任务,如果没有空闲线程则新建线程用于执行任务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class ThreadPoolDemo{ public static void main(String[] args){ ExecutorService threadPool = Executors.newCachedThreadPool(); for(int i = 0; i < 15; i++){ Runnable run = new Runnable(){ @Override public void run(){ Thread t = Thread.currentThread(); System.out.println(t + "正在执行任务。"); } }; threadPool.execute(run); } threadPool.shutdown(); } }
|
上面代码控制台输出如下,发现执行任务有相同线程,说明了线程池对空闲线程灵活回收再利用了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-3,5,main]正在执行任务。 Thread[pool-1-thread-2,5,main]正在执行任务。 Thread[pool-1-thread-4,5,main]正在执行任务。 Thread[pool-1-thread-4,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-2,5,main]正在执行任务。 Thread[pool-1-thread-3,5,main]正在执行任务。 Thread[pool-1-thread-7,5,main]正在执行任务。 Thread[pool-1-thread-8,5,main]正在执行任务。 Thread[pool-1-thread-5,5,main]正在执行任务。 Thread[pool-1-thread-9,5,main]正在执行任务。 Thread[pool-1-thread-11,5,main]正在执行任务。 Thread[pool-1-thread-10,5,main]正在执行任务。 Thread[pool-1-thread-6,5,main]正在执行任务。
|
二、newFixedThreadPool的方式
这种方式创建一个定长线程池,可控制线程的最大并发数,如果超出该并发数,则超出的线程会在队列中等待。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class ThreadPoolDemo { public static void main(String[] args){ ExecutorService threadPool = Executors.newFixedThreadPool(2); for(int i = 0; i < 5; i++){ Runnable run = new Runnable(){ @Override public void run(){ Thread t = Thread.currentThread(); System.out.println(t + "正在执行任务。"); } }; threadPool.execute(run); } threadPool.shutdown(); } }
|
上面代码控制台输出如下,说明执行任务只有2两个线程,控制了并发数,超出的线程会进入等待状态。
1 2 3 4 5
| Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-2,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-2,5,main]正在执行任务。
|
三、newScheduledThreadPool的方式
创建一个定长线程池,但是该线程池支持定时及周期性执行任务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class ThreadPoolDemo{ public static void main(String[] args){ ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2); for(int i = 0 ;i < 5; i++){ Runnable run = new Runnable(){ @Override public void run(){ Thread t = Thread.currentThread(); System.out.println(t + "正在执行任务。"); } }; threadPool.schedule(run, 3, TimeUnit.SECONDS); } threadPool.shutdown(); } }
|
上面代码控制台输出如下,会延迟3秒执行,且执行任务只有2两个线程,控制了并发数,超出的线程会进入等待状态。
1 2 3 4 5 6
| Thread[pool-1-thread-2,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-2,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]正在执行任务。
|
四、newSingleThreadExecutor的方式
创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO,LIFO)执行。这个线程池只是只能存在一个线程,这个线程死后另外一个线程会补上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class ThreadPoolDemo{ public static void main(String[] args){ ExecutorService threadPool = Executors.newSingleThreadExecutor(); for(int i = 0; i < 10 ; i++){ final int index = i; Runnable run = new Runnable(){ @Override public void run(){ Thread t = Thread.currentThread(); System.out.println(t + "正在执行任务。"); if(index == 5) { System.out.println(t + "死掉了。"); t.stop(); } } }; threadPool.execute } threadPool.shutdown(); } }
|
上面代码在控制台输出如下,说明当线程Thread[pool-1-thread-1,5,main]死掉之后,该线程池会创建Thread[pool-1-thread-2,5,main]线程继续执行任务,线程池里只能有一个线程。
1 2 3 4 5 6 7 8 9 10 11
| Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]正在执行任务。 Thread[pool-1-thread-1,5,main]死掉了。 Thread[pool-1-thread-2,5,main]正在执行任务。 Thread[pool-1-thread-2,5,main]正在执行任务。 Thread[pool-1-thread-2,5,main]正在执行任务。 Thread[pool-1-thread-2,5,main]正在执行任务。
|