-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolDemo2.java
More file actions
39 lines (34 loc) · 1 KB
/
Copy pathThreadPoolDemo2.java
File metadata and controls
39 lines (34 loc) · 1 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.*;
/**
* @author : CodeWater
* @create :2022-06-03-20:35
* @Function Description :自定义线程池
* 超过最大办理线程数量会抛出异常
*/
public class ThreadPoolDemo2 {
public static void main(String[] args) {
ExecutorService threadPool = new ThreadPoolExecutor(
2,
5 ,
2L,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(3) ,
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);
//10个顾客请求
try{
for( int i = 0 ; i < 10 ; i++ ){
//执行
threadPool.execute( () -> {
System.out.println( Thread.currentThread().getName() + "办理业务。" );
});
}
}catch( Exception e ){
e.printStackTrace();
}finally{
threadPool.shutdown();
}
}
}