JAVA线程池样例
来源:互联网 发布:vb安装win10 编辑:程序博客网 时间:2024/06/05 05:21
主要抄了三种:
newSingleThreadExecutor
newCachedThreadPool()
newFixedThreadPool(int)
我有几张阿里云幸运券分享给你,用券购买或者升级阿里云相应产品会有特惠惊喜哦!把想要买的产品的幸运券都领走吧!快下手,马上就要抢光了。
package demo.thread;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ThreadMain {public static void main(String[] args) throws Exception {ExecutorService sexecutor = Executors.newSingleThreadExecutor();for (int i = 0; i < 5; i++) {final int no =i;Runnable runnable = new Runnable() {public void run() {try {System.out.println("Single executor into " + no);Thread.sleep(1000L);System.out.println("Single executor end " + no);} catch (InterruptedException e) {e.printStackTrace();}}};sexecutor.execute(runnable);}sexecutor.shutdown();ExecutorService cexecutor = Executors.newCachedThreadPool();for (int i = 0; i < 20; i++) {final int no =i;Runnable runnable = new Runnable() {public void run() {try {System.out.println("Cached executor into " + no);Thread.sleep(1000L);System.out.println("Cached executor end " + no);} catch (InterruptedException e) {e.printStackTrace();}}};cexecutor.execute(runnable);}cexecutor.shutdown();ExecutorService fexecutor = Executors.newFixedThreadPool(5);for (int i = 0; i < 20; i++) {final int no =i;Runnable runnable = new Runnable() {public void run() {try {System.out.println("Fixed executor into " + no);Thread.sleep(1000L);System.out.println("Fixed executor end " + no);} catch (InterruptedException e) {e.printStackTrace();}}};fexecutor.execute(runnable);}fexecutor.shutdown();System.out.println("The main thread end.");}}
原文链接
阅读全文
0 0
- JAVA线程池样例
- JAVA线程池样例
- JAVA线程池样例
- Java线程:什么是线程
- Java线程:线程池
- java线程--线程退出
- JAVA-线程/线程锁
- Java线程:什么是线程
- Java线程:线程中断
- Java线程:线程状态
- Java线程: 线程调度
- Java线程:线程交互
- java--线程--线程池
- java 线程
- Java线程
- java线程
- java线程
- Java线程
- 解决java.lang.IllegalArgumentException: No converter found for return value of type:
- 聊聊"鸡肋"的Java跨平台
- 【JAVA】通过实现java.io.Serializable接口启用类的序列化
- 普及练习场 深度优先搜索 单词接龙
- 网线制作
- JAVA线程池样例
- Java集合——HashMap、HashTable以及ConCurrentHashMap异同比较
- 二叉检索树的C++实现
- python+selenium自动化测试--try except测试断言
- VGG
- utils/decode_rpm.sh
- 12.7 服务器组成详解及企业用户体验优化
- 在C#中使用samus驱动操作MongoDB
- 运行caffe自带的mnist实例详细教