-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFJDemo.java
More file actions
40 lines (32 loc) · 1.21 KB
/
Copy pathFJDemo.java
File metadata and controls
40 lines (32 loc) · 1.21 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
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
public class FJDemo {
public static void main(String[] args) throws InterruptedException {
var max = 100L;
final ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
final List<ForkJoinTask> tasks = new ArrayList<>();
final Instant startTime = Instant.now();
for(var i = 0; i < max; i++){
final var tid = i;
ForkJoinTask t = forkJoinPool.submit(() -> {
try {
System.out.println("Hi from "+tid+" in "+Thread.currentThread());
Thread.sleep(1000);
System.out.println("Bye from "+tid+" in "+Thread.currentThread());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
tasks.add(t);
}
for (ForkJoinTask t : tasks) {
t.join();
}
final Instant endTime = Instant.now();
System.out.println("Time elapsed "+ Duration.between(startTime, endTime).toString());
}
}