Skip to content

Commit 1b2912b

Browse files
自定义ThreadFactory。自定义线程池
1 parent 263c8ad commit 1b2912b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.concurrent.threadpool;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.LinkedBlockingQueue;
5+
import java.util.concurrent.ThreadPoolExecutor;
6+
import java.util.concurrent.TimeUnit;
7+
8+
9+
10+
public class ThreadNum {
11+
12+
13+
14+
// private static final ExecutorService MY_EXECUTOR = Executors.newFixedThreadPool(5);
15+
16+
/**
17+
* 自定义ThreadFactory
18+
* 自定义线程池
19+
*
20+
*/
21+
private static final ExecutorService THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
22+
5, 20,
23+
2, TimeUnit.SECONDS,
24+
new LinkedBlockingQueue<>(1000),
25+
ThreadFactoryImpl.builder().threadName("thead-my-").build());
26+
27+
28+
29+
public static void main(String[] args) {
30+
THREAD_POOL_EXECUTOR.execute(()-> System.out.println(Thread.currentThread().getName()+ ":first thread."));
31+
THREAD_POOL_EXECUTOR.execute(()-> System.out.println(Thread.currentThread().getName()+ ":second thread."));
32+
THREAD_POOL_EXECUTOR.shutdown();
33+
}
34+
35+
36+
37+
38+
}

0 commit comments

Comments
 (0)