当前位置:网站首页>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();
}
}
边栏推荐
- Mathematical knowledge: step Nim game game game theory
- Give you an array numbers that may have duplicate element values. It was originally an array arranged in ascending order, and it was rotated once according to the above situation. Please return the sm
- 数学知识:台阶-Nim游戏—博弈论
- [untitled]
- LDC Build Shared Library
- 英语常用词汇
- openresty 缓存
- Mathematical knowledge: divisible number inclusion exclusion principle
- 电信客户流失预测挑战赛
- 数学知识:Nim游戏—博弈论
猜你喜欢

MySQL foundation 04 MySQL architecture

Basic remote connection tool xshell

MySQL basics 03 introduction to MySQL types

给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】

C#应用程序界面开发基础——窗体控制(3)——文件类控件

High-Resolution Network (篇一):原理刨析

Why can't the start method be called repeatedly? But the run method can?
![[Arduino experiment 17 L298N motor drive module]](/img/e2/4511eaa942e4a64c8ca2ee70162785.jpg)
[Arduino experiment 17 L298N motor drive module]
![[fh-gfsk] fh-gfsk signal analysis and blind demodulation research](/img/8a/8ca80f51a03341c982d52980c54b01.png)
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research

Daily topic: movement of haystack
随机推荐
[shutter] animation animation (the core class of shutter animation | animation | curvedanimation | animationcontroller | tween)
Do not log in or log in to solve the problem that the Oracle database account is locked.
简易分析fgui依赖关系工具
Vim 9.0正式发布!新版脚本执行速度最高提升100倍
【面试题】1369- 什么时候不能使用箭头函数?
Test shift right: Elk practice of online quality monitoring
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research
Using tensorboard to visualize the model, data and training process
Scheme and practice of cold and hot separation of massive data
[Arduino experiment 17 L298N motor drive module]
产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
一比特苦逼程序員的找工作經曆
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
C language course information management system
The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
Top ten regular spot trading platforms 2022
[技术发展-23]:DSP在未来融合网络中的应用
[shutter] animation animation (shutter animation type | the core class of shutter animation)
Dotconnect for PostgreSQL data provider
Telecom Customer Churn Prediction challenge