-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduledExecutorDemo.java
More file actions
36 lines (30 loc) · 1.04 KB
/
ScheduledExecutorDemo.java
File metadata and controls
36 lines (30 loc) · 1.04 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
package ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorDemo {
public static void main(String[] args) {
ScheduledExecutorService executor= Executors.newScheduledThreadPool(2);
executor.scheduleAtFixedRate(new ProbeTask(),1000,2000, TimeUnit.MILLISECONDS);
executor.scheduleAtFixedRate(new AnotherOne(),1000,2000, TimeUnit.MILLISECONDS);
try{
if(!executor.awaitTermination(10000,TimeUnit.MILLISECONDS)){
executor.shutdownNow();
}
}catch(InterruptedException e){
executor.shutdownNow();
}
}
}
class ProbeTask implements Runnable{
@Override
public void run(){
System.out.println("probing1.. with "+Thread.currentThread().getName());
}
}
class AnotherOne implements Runnable{
@Override
public void run(){
System.out.println("probing2.. with "+Thread.currentThread().getName());
}
}