-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchedule.java
More file actions
37 lines (30 loc) · 934 Bytes
/
Copy pathSchedule.java
File metadata and controls
37 lines (30 loc) · 934 Bytes
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
package concurrentExecutor;
import java.time.LocalTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
class HelloTask implements Runnable{
String name;
public HelloTask(String name){
this.name = name;
}
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("Hello, " + name + " ! It is "+ LocalTime.now());
try {
Thread.sleep(1000);
} catch(InterruptedException e){
}
}
}
}
public class Schedule {
public static void main(String[] args) throws InterruptedException {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
executor.scheduleAtFixedRate(new HelloTask("bob"),2, 5, TimeUnit.SECONDS);
executor.scheduleWithFixedDelay(new HelloTask("Alice"),2, 5, TimeUnit.SECONDS);
Thread.sleep(10000);
executor.shutdown();
}
}