当前位置:网站首页>Three methods of thread pool
Three methods of thread pool
2022-06-22 06:18:00 【Kuxiaoya】
Three ways to pool threads
Executors.newSingleThreadExecutor();Single thread
Executors.newFixedThreadPool(5);Fixed number of threads
Executors.newCachedThreadPool();Buffer pool , Scalable
Executors.newSingleThreadExecutor(); Single thread
package pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** * Executors Tool class 、3 The big way */
public class Demo01 {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newSingleThreadExecutor();// Single thread
try {
for (int i = 0; i < 10; i++) {
// After using the thread pool , Use thread pool to create threads
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+"OK!");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// Out of thread pool , Program end , Close thread pool
threadPool.shutdown();
}
}
}
Executors.newFixedThreadPool(5); Fixed number of threads
package pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** * Executors Tool class 、3 The big way */
public class Demo01 {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(5); // Create a fixed thread pool size
try {
for (int i = 0; i < 10; i++) {
// After using the thread pool , Use thread pool to create threads
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" OK!");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// Out of thread pool , Program end , Close thread pool
threadPool.shutdown();
}
}
}
Executors.newCachedThreadPool(); Buffer pool , Scalable
1package pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** * Executors Tool class 、3 The big way */
public class Demo01 {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newCachedThreadPool();// scalable , If you are strong, you will be strong , If you are weak, you will be weak
try {
for (int i = 0; i < 100; i++) {
// After using the thread pool , Use thread pool to create threads
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" OK!");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// Out of thread pool , Program end , Close thread pool
threadPool.shutdown();
}
}
}
边栏推荐
猜你喜欢

C#中的数组及Foreach遍历

Discrete PID control based on MATLAB

Research on dynamics and control of single ball robot

Performance comparison and analysis

Pyg tutorial (7): dissecting neighborhood aggregation

Surfer grid file clipping

生信文献学习(part1)--PRECISE: a ... approach to transfer predictors of drug response from pre-clinical ...

类加载内存分析

No business series 7: removing spots from old photos

Current harmonic suppression strategy of grid connected inverter with PIR controller regulator
随机推荐
Vertical maximum and minimum and horizontal maximum and minimum greatest(), least(), max(), min()
Shengxin visualization (Part2) -- box diagram
BlockingQueue四组API
On the definition of jinja2 macro
Idea run scope locally
Logback custom pattern parameter resolution
Breakthrough in rich device platform: dayu200 based on rk3568 enters the openharmony 3.1 release trunk
基于卫星测深的牙买加沿岸水深测量
Little bear school bearpi HM micro officially integrated into openharmony trunk
Bathymetry along Jamaica coast based on Satellite Sounding
e.hash & oldCap == 0 详细解读
雷达导引头伺服系统的建模与仿真
Shengxin visualization (Part4) -- correlation diagram
Huiding technology gr551x series development board supports openharmony
四大函数式接口(必需掌握)
Discrete PID control based on MATLAB
Paging tool class pageutil < t >
laravel Excel 3.1 列宽设置不起作用
New GDI functions and functions introduced in MiniGUl version 1.1.0 (II)
相干声呐GeoSwath的综述