java线程池代码模板

java 线程池代码模板

介绍

  java 中创建默认的线程池的代码模板。

代码

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.lin.demo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* 线程池
*/
public class ThreadPool {

/**
* 缓存线程池
*/
public void newCachedThreadPool() {
ExecutorService executorService = Executors.newCachedThreadPool();

for (int i = 0; i < 5; i++) {
final int index = i;
executorService.execute(new Runnable() {
public void run() {
try {
SimpleDateFormat sdf = new SimpleDateFormat(
"HH:mm:ss");
System.out.println("运行时间: " +
sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
executorService.shutdown();
}

/**
* 单线程线程池
*/
public void newSingleThreadExecutor() {
ExecutorService executorService = Executors.newSingleThreadExecutor();

for (int i = 0; i < 5; i++) {
final int index = i;
executorService.execute(new Runnable() {
public void run() {
try {
SimpleDateFormat sdf = new SimpleDateFormat(
"HH:mm:ss");
System.out.println("运行时间: " +
sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
executorService.shutdown();
}

/**
* 定长线程池
*/
public void newFixedThreadPool() {
ExecutorService executorService = Executors.newFixedThreadPool(3);

for (int i = 0; i < 5; i++) {
final int index = i;
executorService.execute(new Runnable() {
public void run() {
try {
SimpleDateFormat sdf = new SimpleDateFormat(
"HH:mm:ss");
System.out.println("运行时间: " +
sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}

/**
* 定时执行的线程池
*/
public void newScheduledThreadPool() {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
SimpleDateFormat sdf = new SimpleDateFormat(
"HH:mm:ss");
for (int i = 0; i < 5; i++) {
final int index = i;
System.out.println("提交时间: " + sdf.format(new Date()));
executorService.schedule(new Runnable() {
public void run() {
try {

System.out.println("运行时间: " +
sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 3, TimeUnit.SECONDS);
}
}

/**
* 周期执行的线程池
*
* @throws InterruptedException
*/
public void newScheduledThreadPool2() throws InterruptedException {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
SimpleDateFormat sdf = new SimpleDateFormat(
"HH:mm:ss");
// for (int i = 0; i < 5; i++) {
final int index = 1;
System.out.println("提交时间: " + sdf.format(new Date()));
executorService.scheduleAtFixedRate(new Runnable() {
public void run() {
try {

System.out.println("运行时间: " +
sdf.format(new Date()) + " " + index + " " + Thread.currentThread().getName());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 1, 3, TimeUnit.SECONDS);
//主线程等待10秒钟后关闭
Thread.sleep(10000);
// }
executorService.shutdown();
}
}