当前位置:网站首页>JUC thread scheduling
JUC thread scheduling
2022-07-03 01:33:00 【W_ Meng_ H】
package org.meng.juc;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/*
* One 、 Thread pool : Provides a thread queue , The queue holds all waiting threads . Avoid the creation and destruction of additional costs , Improved response speed .
*
* Two 、 The architecture of thread pool :
* java.util.concurrent.Executor : Root interface responsible for thread usage and scheduling
* |--**ExecutorService A subinterface : Main interface of thread pool
* |--ThreadPoolExecutor Implementation class of thread pool
* |--ScheduledExecutorService A subinterface : Responsible for the scheduling of threads
* |--ScheduledThreadPoolExecutor : Inherit ThreadPoolExecutor, Realization ScheduledExecutorService
*
* 3、 ... and 、 Tool class : Executors
* ExecutorService newFixedThreadPool() : Create a fixed-size thread pool
* ExecutorService newCachedThreadPool() : Cache thread pool , The number of thread pools is not fixed , The quantity can be changed automatically according to the demand .
* ExecutorService newSingleThreadExecutor() : Create a single thread pool . There is only one thread in the thread pool
*
* ScheduledExecutorService newScheduledThreadPool() : Create a fixed size thread , Tasks can be executed late or on a regular basis .
*/
public class TestScheduledThreadPool {
public static void main(String[] args) throws Exception {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
for (int i = 0; i < 5; i++) {
Future<Integer> result = pool.schedule(new Callable<Integer>(){
@Override
public Integer call() throws Exception {
int num = new Random().nextInt(100);// Generate random number
System.out.println(Thread.currentThread().getName() + " : " + num);
return num;
}
}, 1, TimeUnit.SECONDS);
System.out.println(result.get());
}
pool.shutdown();
}
}
边栏推荐
- MySQL foundation 04 MySQL architecture
- 看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9
- [interview question] 1369 when can't I use arrow function?
- Kivy教程大全之 创建您的第一个kivy程序 hello word(教程含源码)
- kivy教程之在 Kivy App 中使用 matplotlib 的示例
- MySQL --- 数据库查询 - 基本查询
- 按键精灵打怪学习-自动回城路线的判断
- [自我管理]时间、精力与习惯管理
- Look at how clothing enterprises take advantage of the epidemic
- Makefile中wildcard、patsubst、notdir的含义
猜你喜欢
leetcode 2097 — 合法重新排列数对
【面试题】1369- 什么时候不能使用箭头函数?
Database SQL language 02 connection query
Database SQL language 01 where condition
What is tone. Diao's story
MySQL basics 03 introduction to MySQL types
Top ten regular spot trading platforms 2022
Meituan dynamic thread pool practice ideas, open source
[Androd] Gradle 使用技巧之模块依赖替换
Wireshark data analysis and forensics a.pacapng
随机推荐
Is there anything in common between spot gold and spot silver
[technology development-23]: application of DSP in future converged networks
After reading this article, I will teach you to play with the penetration test target vulnhub - drivetingblues-9
Steps to obtain SSL certificate private key private key file
2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
How is the mask effect achieved in the LPL ban/pick selection stage?
Key wizard play strange learning - front desk and Intranet send background verification code
【面试题】1369- 什么时候不能使用箭头函数?
tail -f 、tail -F、tailf的区别
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research
The difference between tail -f, tail -f and tail
Arduino dy-sv17f automatic voice broadcast
LDC Build Shared Library
Using tensorboard to visualize the model, data and training process
Niu Ke swipes questions and clocks in
音程的知识的总结
[技术发展-23]:DSP在未来融合网络中的应用
Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées