当前位置:网站首页>线程池--
线程池--
2022-04-22 14:07:00 【风生u】
线程池简介







代码实现
package 线程池;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
/** * @author: fs * @date: 2022/4/20 14:45 * @Description: */
public class MyThreadPool {
// 1.描述一个任务.直接使用 Runnable,不需要额外创建类了.
// 2.使用一个数据结构来组织若干个任务.
private BlockingQueue<Runnable> queue = new LinkedBlockingDeque<>();
//3.描述一个线程,工作线程的功能就是从任务队列中取任务并执行.
static class Worker extends Thread {
//当前线程池中有若干个 worker 线程~~这些线程内部都持有了上述的任务队列.
private BlockingQueue<Runnable> queue = null;
public Worker(BlockingQueue<Runnable> queue) {
this.queue = queue;
}
@Override
public void run() {
//就需要能够拿到上面的队列
while (true) {
try {
//循环的去获取任务队列中的任务.
//这里如果队列为空,就直接阻塞.如果队列非空,就获取到里面的内容·
Runnable runnable = queue.take();
//获取到之后,就执行任务.
runnable.run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 4.创建一个数据结构来组织若干个线程.
public List<Thread> workers = new ArrayList<>();
public MyThreadPool(int n) {
//在构造方法中,创建出若干个线程,放到上述的数组中.
for (int i = 0; i < n; i++) {
Worker worker = new Worker(queue);
worker.start();
workers.add(worker);
}
}
// 5.创建一个方法,能够允许程序猿来放任务到线程池中.
public void submit(Runnable runnable) {
try {
queue.put(runnable);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Demo26 {
public static void main(String[] args) {
MyThreadPool pool = new MyThreadPool(10);
for (int i = 0; i < 100; i++) {
pool.submit(new Runnable() {
@Override
public void run() {
System.out.println("hello thneadpool");
}
});
}
}
}
版权声明
本文为[风生u]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_56182317/article/details/124296076
边栏推荐
- uniapp运行到小程序模拟器的方法 - uniapp开启微信开发者工具预览支持 - HBuilderX
- "Enterprise service" of digital operation and management of Industrial Park
- 獲取數據庫中數值時,數據庫有值,卻為空??
- FastDFS 安装和配置
- TensorFlow-ValueError: ‘images‘ contains no shape
- TensorFlow-ValueError: ‘images‘ contains no shape
- BCC-stackcount
- 树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
- 2022年危险化学品经营单位主要负责人考试练习题及答案
- Go down and continue to go down. Xiaohui's fund has lost 640000...
猜你喜欢

机器越“智能”,数据标注员越容易被淘汰?丨曼孚科技

Imeta: integrating macroomics to re understand life and Environmental Sciences

2022茶艺师(中级)考试模拟100题及模拟考试

回忆,这一年(华师918血泪贴)

Cannot read property 'forceupdate' of undefined - wechat developer tool reports an error

【论文笔记】Vision Transformers for Dense Prediction

獲取數據庫中數值時,數據庫有值,卻為空??

Redis connection tool cannot connect to redis in docker

plsql developer文件编码格式设置

一个解法解决所有力扣买卖股票问题
随机推荐
A solution to the problem of buying and selling stocks by force deduction
Brief description of three elements of LAN characteristics
Mysql数据库转存sql文件
How to use openfeign to call the third-party interface
专家系统的开发环境
游戏合作伙伴专题:BreederDAO 与 Fancy Birds 合作达到新高度
Detailed explanation of channel implementation of cocoyaxi Library
Is it effective to control caching through meta information in HTML files? Is it used much at present?
Method of page nesting
2022危险化学品经营单位安全管理人员操作证考试题及模拟考试
分析电脑使用变慢的七大原因
处理高并发问题思路
FastDFS 安装和配置
2022 examination questions and simulation examination of operation certificate for safety management personnel of hazardous chemical business units
微信小程序商城项目实战(第六篇:商品搜索)
產業園區數字化運營管理之“精准招商”篇
Sihao X6 safety configuration information exposure will be equipped with adaptive cruise
银行为什么要上堡垒机?选择哪家好?有案例吗?
Analyze the seven reasons for the slow use of computers
嵌入式软件bug从哪来,怎么去