-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolDemo1.java
More file actions
39 lines (32 loc) · 1.04 KB
/
Copy pathThreadPoolDemo1.java
File metadata and controls
39 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author : CodeWater
* @create :2022-06-03-19:56
* @Function Description :线程池的使用方式
*/
public class ThreadPoolDemo1 {
public static void main( String[] args ) {
//一池五线程 5个
ExecutorService threadPool1 = Executors.newFixedThreadPool( 5 );
//一池一线程 1
ExecutorService threadPool2 = Executors.newSingleThreadExecutor();
//一池可扩容线程
ExecutorService threadPool3 = Executors.newCachedThreadPool();
//10个顾客请求
try{
for( int i = 0 ; i < 30 ; i++ ){
//执行
threadPool3.execute( () -> {
System.out.println( Thread.currentThread().getName() + "办理业务" );
});
}
}catch( Exception e){
e.printStackTrace();
}finally{
//关闭
threadPool3.shutdown();
}
}
}