-
Notifications
You must be signed in to change notification settings - Fork 3
/
ScheduledThreadPool.java
38 lines (24 loc) · 1011 Bytes
/
ScheduledThreadPool.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
import java.util.Calendar;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import static java.util.concurrent.TimeUnit.SECONDS;
public class ScheduledThreadPool {
public static void main(String[] args) {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
executorService.schedule(()->{
System.out.println(Calendar.getInstance().getTime());
}, 3, SECONDS);
executorService.scheduleAtFixedRate(()->{
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
},1,1,SECONDS);
executorService.scheduleWithFixedDelay(()->{
System.out.println(Calendar.getInstance().getTime());
},4,1, SECONDS);
}
}