JAVA多线程——线程池
1. 固定线程池:Executors.newFixedThreadPool(5);
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author huangdh
* @version 1.0
* @description:
* @date 2022-10-30 22:31
*/
public class ThreadPoolDemo {
public static void main(String[] args) {
// 固定线程池
ExecutorService threadPool1 = Executors.newFixedThreadPool(5);
// 一池一线程
ExecutorService threadPool2 = Executors.newSingleThreadExecutor();
// 可扩容线程池
ExecutorService threadPool3 = Executors.newCachedThreadPool();
// 10个顾客请求
try {
for (int i = 0; i < 10; i++) {
threadPool1.execute(()->{
System.out.println(Thread.currentThread().getName() + " 正在办理业务");
});
/*threadPool2.execute(()->{
System.out.println(Thread.currentThread().getName() + " 正在办理业务");
});*/
/*threadPool3.execute(()->{
System.out.println(Thread.currentThread().getName() + " 正在办理业务");
});*/
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool3.shutdown();
}
}
}
pool-1-thread-2 正在办理业务
pool-1-thread-3 正在办理业务
pool-1-thread-1 正在办理业务
pool-1-thread-4 正在办理业务
pool-1-thread-4 正在办理业务
pool-1-thread-5 正在办理业务
pool-1-thread-3 正在办理业务
pool-1-thread-2 正在办理业务
pool-1-thread-4 正在办理业务
pool-1-thread-1 正在办理业务
2. 单例线程池:Executors.newSingleThreadExecutor();
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author huangdh
* @version 1.0
* @description:
* @date 2022-10-30 22:31
*/
public class ThreadPoolDemo {
public static void main(String[] args) {
// 固定线程池
ExecutorService threadPool1 = Executors.newFixedThreadPool(5);
// 一池一线程
ExecutorService threadPool2 = Executors.newSingleThreadExecutor();
// 可扩容线程池
ExecutorService threadPool3 = Executors.newCachedThreadPool();
// 10个顾客请求
try {
for (int i = 0; i < 10; i++) {
/*threadPool1.execute(()->{
System.out.println(Thread.currentThread().getName() + " 正在办理业务");
});*/
threadPool2.execute(()->{
System.out.println(Thread.currentThread().getName() + " 正在办理业务");
});
/*threadPool3.execute(()->{
System.out.println(Thread.currentThread().getName() + " 正在办理业务");
});*/
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool3.shutdown();
}
}
}
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
3. 可扩容线程池:Executors.newCachedThreadPool();
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author huangdh
* @version 1.0
* @description:
* @date 2022-10-30 22:31
*/
public class ThreadPoolDemo {
public static void main(String[] args) {
// 固定线程池
ExecutorService threadPool1 = Executors.newFixedThreadPool(5);
// 一池一线程
ExecutorService threadPool2 = Executors.newSingleThreadExecutor();
// 可扩容线程池
ExecutorService threadPool3 = Executors.newCachedThreadPool();
// 10个顾客请求
try {
for (int i = 0; i < 10; i++) {
/*threadPool1.execute(()->{
System.out.println(Thread.currentThread().getName() + " 正在办理业务");
});*/
/*threadPool2.execute(()->{
System.out.println(Thread.currentThread().getName() + " 正在办理业务");
});*/
threadPool3.execute(()->{
System.out.println(Thread.currentThread().getName() + " 正在办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool3.shutdown();
}
}
}
pool-3-thread-1 正在办理业务
pool-3-thread-4 正在办理业务
pool-3-thread-3 正在办理业务
pool-3-thread-2 正在办理业务
pool-3-thread-5 正在办理业务
pool-3-thread-6 正在办理业务
pool-3-thread-7 正在办理业务
pool-3-thread-5 正在办理业务
pool-3-thread-2 正在办理业务
pool-3-thread-6 正在办理业务
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构