Repository navigation
Expand file tree
/
Copy pathThreadPoolExecutorTest.java
More file actions
executable file
·113 lines (100 loc) · 3.23 KB
/
Copy pathThreadPoolExecutorTest.java
File metadata and controls
executable file
·113 lines (100 loc) · 3.23 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package thread_pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ThreadPoolExecutorTest {
public static void main(String[] args) {
Thread t = new Thread();
t.yield();
// 缓存线程池,内部线程数最大为Integer.MAX_VALUE
testCachedThreadPool();
// 定长线程池
// testFixedThreadPool();
// 定长线程池--1
// testScheduledThreadPool_1();
// 定长线程池--2
// testScheduledThreadPool_2();
// 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务
// testSingleThredPool();
}
/**
* 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定 顺序(FIFO, LIFO, 优先级)执行。
*/
private static void testSingleThredPool() {
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
final int index = i;
singleThreadExecutor.execute(new Runnable() {
public void run() {
try {
System.out.println(Thread.currentThread());
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
/**
* 创建一个定长线程池,支持定时及周期性任务执行。定期执行示例代码如下:
*/
private static void testScheduledThreadPool_2() {
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
public void run() {
System.out.println("delay 1 seconds, and excute every 3 seconds");
}
}, 1, 2, TimeUnit.SECONDS);
}
/**
* 创建一个定长线程池,支持定时及周期性任务执行。延迟执行示例代码如下
*/
private static void testScheduledThreadPool_1() {
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
scheduledThreadPool.schedule(new Runnable() {
public void run() {
System.out.println("delay 3 seconds");
}
}, 3, TimeUnit.SECONDS);
}
/**
* 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程
*/
private static void testCachedThreadPool() {
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
final int index = i;
try {
Thread.sleep(index * 100);
} catch (InterruptedException e) {
e.printStackTrace();
}
cachedThreadPool.execute(new Runnable() {
public void run() {
System.out.println(Thread.currentThread());
}
});
}
}
/**
* 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列(LinkedBlockingQueue)中等待
*/
private static void testFixedThreadPool() {
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
for (int i = 0; i < 30; i++) {
final int index = i;
fixedThreadPool.execute(new Runnable() {
public void run() {
try {
System.out.println(Thread.currentThread().getId());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}