Executors创建四种线程池

newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池执行任务。

 

 
package com.xhx.java;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

// Executors四种线程池
public class AppTest
{
private Runnable myRunnable = new Runnable() {
@Override
public void run() {
try {
//Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + " run");
} catch (Exception e) {
e.printStackTrace();
}
}
};

private Runnable myRunnableSleep = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName() + " run");
} catch (Exception e) {
e.printStackTrace();
}
}
};


/**
* newCachedThreadPool:创建可缓存的线程池,如果线程池中的线程在60秒未被使用就将被移除,在执行新的任务时,
* 当线程池中有之前创建的可用线程就重用可用线程,否则就新建一条线程
* public static ExecutorService newCachedThreadPool() {
* return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
* 60L, TimeUnit.SECONDS,
* new SynchronousQueue<Runnable>());
* }
*/
@Test
public void testCachedThreadPool() throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();//线程池里面的线程数会动态的变化,并可在线程被重用前重用
for(int i = 0; i<=20;i++){
executorService.execute(myRunnable);
}

Thread.sleep(100000);
}
/*
运行结果:出现可重用线程
pool-1-thread-2 run
pool-1-thread-1 run
pool-1-thread-3 run
pool-1-thread-3 run
pool-1-thread-2 run
pool-1-thread-4 run
pool-1-thread-3 run
pool-1-thread-1 run
pool-1-thread-2 run
pool-1-thread-4 run
pool-1-thread-3 run
pool-1-thread-2 run
pool-1-thread-1 run
pool-1-thread-5 run
pool-1-thread-4 run
pool-1-thread-6 run
pool-1-thread-7 run
pool-1-thread-8 run
pool-1-thread-9 run
pool-1-thread-10 run
pool-1-thread-11 run
*/


/**
* newScheduledThreadPool:创建一个可延迟执行或定期执行的线程池
*/
@Test
public void testScheduledThreadPool() throws InterruptedException {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);
//5s第一次执行,之后3秒执行一次
executorService.scheduleAtFixedRate(myRunnable,5,3,TimeUnit.SECONDS);

Thread.sleep(20000);
}

/**
* newSingleThreadExecutor:创建一个单线程的Executor,如果该线程因为异常而结束就新建一条线程来继续执行后续的任务
*/
@Test
public void testSingleThreadExecutor() throws InterruptedException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
for(int i = 0; i<=20;i++){
executorService.execute(myRunnable);
}
Thread.sleep(20000);
}

/**
* 创建可重用且固定线程数的线程池,如果线程池中的所有线程都处于活动状态,
* 此时再提交任务就在队列中等待,直到有可用线程;如果线程池中的某个线程由于异常而结束时,线程池就会再补充一条新线程。
*/
@Test
public void testFixedThreadPool() throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(10);
for(int i = 0; i<=20;i++){
executorService.execute(myRunnableSleep);
}
Thread.sleep(200000);
}
}

posted on   我是司  阅读(1036)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示