forked from davidjava1991/java17CompleteCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample1.java
More file actions
30 lines (28 loc) · 968 Bytes
/
Copy pathExample1.java
File metadata and controls
30 lines (28 loc) · 968 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
package com.david.class165;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
public class Example1 {
public static void main(String[] args) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
Future<String> s1 = executor.submit(() -> {
System.out.println("Thread1 started");
Thread.sleep(500);
return "task1 done";
});
Future<String> s2 = executor.submit(() -> {
System.out.println("Thread2 started");
Thread.sleep(500);
return "task2 done";
});
System.out.println("Pool size : "+executor.getPoolSize());
System.out.println("Queue size : "+executor.getQueue().size());
try {
System.out.println(s1.get());
System.out.println(s2.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}