forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleThreadPoolExecutor.java
More file actions
63 lines (50 loc) · 2.05 KB
/
SimpleThreadPoolExecutor.java
File metadata and controls
63 lines (50 loc) · 2.05 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package modern.challenge;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class SimpleThreadPoolExecutor implements Runnable {
private final int taskId;
public SimpleThreadPoolExecutor(int taskId) {
this.taskId = taskId;
}
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
// log ex
}
System.out.println("Executing task " + taskId + " via " + Thread.currentThread().getName());
}
public static void main(String[] args) {
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(5);
final AtomicInteger counter = new AtomicInteger();
ThreadFactory threadFactory = (Runnable r) -> {
System.out.println("Creating a new Cool-Thread-" + counter.incrementAndGet());
return new Thread(r, "Cool-Thread-" + counter.get());
};
RejectedExecutionHandler rejectedHandler = (Runnable r, ThreadPoolExecutor executor) -> {
if (r instanceof SimpleThreadPoolExecutor) {
SimpleThreadPoolExecutor task = (SimpleThreadPoolExecutor) r;
System.out.println("Rejecting task " + task.taskId);
}
};
ThreadPoolExecutor executor = new ThreadPoolExecutor(
10, 20, 1, TimeUnit.SECONDS, queue, threadFactory, rejectedHandler);
for (int i = 0; i < 50; i++) {
executor.execute(new SimpleThreadPoolExecutor(i));
}
executor.shutdown();
try {
executor.awaitTermination(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
// log ex
}
}
}