当前位置:网站首页>Thread pool--
Thread pool--
2022-04-22 14:08:00 【Aeolian u】
Thread pool
Introduction to thread pool







Code implementation
package Thread pool ;
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. Describe a task . Use it directly Runnable, There is no need to create additional classes .
// 2. Use a data structure to organize several tasks .
private BlockingQueue<Runnable> queue = new LinkedBlockingDeque<>();
//3. Describe a thread , The function of a worker thread is to take a task from a task queue and execute it .
static class Worker extends Thread {
// There are several threads in the current thread pool worker Threads ~~ The above task queues are held inside these threads .
private BlockingQueue<Runnable> queue = null;
public Worker(BlockingQueue<Runnable> queue) {
this.queue = queue;
}
@Override
public void run() {
// You need to be able to get to the queue above
while (true) {
try {
// Cycle to get the tasks in the task queue .
// Here, if the queue is empty , Just block . If the queue is not empty , Just get the content inside ·
Runnable runnable = queue.take();
// After acquisition , Just carry out the task .
runnable.run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 4. Create a data structure to organize several threads .
public List<Thread> workers = new ArrayList<>();
public MyThreadPool(int n) {
// In the constructor , Create several threads , Put it in the above array .
for (int i = 0; i < n; i++) {
Worker worker = new Worker(queue);
worker.start();
workers.add(worker);
}
}
// 5. Create a method , It allows programs to put tasks into the thread pool .
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");
}
});
}
}
}
版权声明
本文为[Aeolian u]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221407012605.html
边栏推荐
- What is adapter mode?
- makefile 调用bash脚本遇到的坑
- 【pytorch】自己实现精简版YOLOV3【五】,实现YOLOV3损失函数(一)
- 【Zeekr_Tech】ROS/ROS 2介绍
- [PHP] MVCs concept (easy to understand)
- Independent station operation | 6 Facebook promotion tips, do you know?
- 树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
- Osgearth configuring Map Resources
- Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
- Chapitre sur la promotion précise des investissements dans la gestion de l'exploitation numérique des parcs industriels
猜你喜欢

Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s

二月份,我靠这一份PDF文档面试BAT,没想到竟然收到了5个offer

【Zeekr_Tech】ROS/ROS 2介绍

"Precise investment promotion" of digital operation and management of Industrial Park
![[robot learning] learning record of mobile robot odometer](/img/cb/37155ad01a9f998a0477becbdcb9fa.jpg)
[robot learning] learning record of mobile robot odometer

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

osgEarth配置地图资源

Super user of oceanbase - system tenant sys

P2B论文复现——点云学习记录
![[zeekr_tech] Introduction to ros/ros 2](/img/74/471261d2eb9a41d132dae9af323b7c.png)
[zeekr_tech] Introduction to ros/ros 2
随机推荐
Independent station operation | 6 Facebook promotion tips, do you know?
银行为什么要上堡垒机?选择哪家好?有案例吗?
2022年 MathorCup 思路分享
LeetCode-819 最常见的单词
LeetCode-386 字典序排数
Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!
Redis相比memcached
Markdown Icon
Customization of AoYa Dao community pledge mining DAPP system
处理高并发问题思路
Qiniu school asks you to download Dragonfly gold to open an account before you can continue to study? Can I download it? Is it safe?
获取数据库中数值时,数据库有值,却为空??
Golang: package management
Chapitre sur la promotion précise des investissements dans la gestion de l'exploitation numérique des parcs industriels
【论文笔记】Vision Transformers for Dense Prediction
Genesis creative comics [stable pass]
BCC-stackcount
PLSQL developer file encoding format setting
Leetcode-386 dictionary order
獲取數據庫中數值時,數據庫有值,卻為空??