forked from zaiyunduan123/Java-Summarize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduledThreadPoolTest.java
More file actions
60 lines (46 loc) · 1.56 KB
/
ScheduledThreadPoolTest.java
File metadata and controls
60 lines (46 loc) · 1.56 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
package pool;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Auther: Jesper
* @Date: 2019/1/24 10:20
* @Description:
*/
public class ScheduledThreadPoolTest {
public static void main(String[] args) throws InterruptedException {
//创建大小为5的线程池
ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(5);
for (int i = 0; i < 3; i++) {
Task worker = new Task("task-" + i);
//周期性执行,每5秒执行一次
scheduledExecutorService.scheduleAtFixedRate(worker, 0, 5, TimeUnit.SECONDS);
}
Thread.sleep(10000);
System.out.println("Shutting down executor...");
scheduledExecutorService.shutdown();
boolean isDone;
do {
isDone = scheduledExecutorService.awaitTermination(1, TimeUnit.DAYS);
System.out.println("awaitTermination...");
} while (!isDone);
System.out.println("Finished all threads");
}
}
class Task implements Runnable {
private String name;
public Task(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println("name = " + name + ", startTime = " + new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("name = " + name + ", endTime = " + new Date());
}
}