当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

How is the mask effect achieved in the LPL ban/pick selection stage?

【数据挖掘】任务5:K-means/DBSCAN聚类:双层正方形

Learn the five skills you need to master in cloud computing application development
![[technology development-23]: application of DSP in future converged networks](/img/2e/f39543a18a8f58b1d341ce72cc4427.png)
[technology development-23]: application of DSP in future converged networks

Telecom Customer Churn Prediction challenge
![[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道](/img/c6/9aee30cb935b203c7c62b12c822085.jpg)
[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道

Daily topic: movement of haystack

C#应用程序界面开发基础——窗体控制(3)——文件类控件
![[interview question] 1369 when can't I use arrow function?](/img/7f/84bba39965b4116f20b1cf8211f70a.png)
[interview question] 1369 when can't I use arrow function?

串口抓包/截断工具的安装及使用详解
随机推荐
[androd] module dependency replacement of gradle's usage skills
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
Related concepts of GDB in embedded system
Thinkphp+redis realizes simple lottery
2022 cable crane driver examination registration and cable crane driver certificate examination
C application interface development foundation - form control (4) - selection control
Common English Vocabulary
[understanding of opportunity -36]: Guiguzi - flying clamp chapter - prevention against killing and bait
MySQL --- 数据库查询 - 条件查询
Kivy tutorial - example of using Matplotlib in Kivy app
High-Resolution Network (篇一):原理刨析
The difference between tail -f, tail -f and tail
Canvas drawing -- bingdd
Force buckle 204 Count prime
【C语言】指针与数组笔试题详解
[shutter] animation animation (shutter animation type | the core class of shutter animation)
传输层 TCP主要特点和TCP连接
LeetCode 987. Vertical order transverse of a binary tree - Binary Tree Series Question 7
Using tensorboard to visualize the model, data and training process
leetcode 6103 — 从树中删除边的最小分数