-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.java
More file actions
108 lines (99 loc) · 3.52 KB
/
ThreadPool.java
File metadata and controls
108 lines (99 loc) · 3.52 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import static java.lang.Thread.sleep;
class PoolRunnable implements Runnable {
ArrayBlockingQueue<Runnable> blockingQueue;
Thread thread = null;
boolean isStop;
public PoolRunnable( ArrayBlockingQueue<Runnable> blockingQueue) {
this.blockingQueue = blockingQueue;
}
@Override
public void run() {
thread = Thread.currentThread();
while(!isStop) {
try {
Runnable rb = blockingQueue.take();
rb.run();
} catch (InterruptedException e) {
System.out.println("interrupted exception in poolthread " + Thread.currentThread().getName());
e.printStackTrace();
}
}
}
public synchronized void stop() {
this.isStop = true;
//System.out.print(Thread.currentThread().getName());
thread.interrupt();
}
}
public class ThreadPool {
ArrayBlockingQueue<Runnable> blockingQueue;
ArrayList<PoolRunnable> poolList = new ArrayList<PoolRunnable>();
HashMap<Long,Thread> producerThreadMap = new HashMap<Long,Thread>();
private boolean isStopped = false;
int capacity;
public ThreadPool(int capacity, int threads) {
this.capacity = capacity;
blockingQueue = new ArrayBlockingQueue<Runnable>(capacity);
for(int i = 0; i < threads;i++) {
PoolRunnable runnable = new PoolRunnable(blockingQueue);
poolList.add(runnable);
String threadName = "Thread " +Integer.toString(i);
Thread t = new Thread(runnable, threadName);
t.start();
}
}
public void execute(Runnable runnable) {
if(this.isStopped) {
//throw new IllegalStateException("ThreadPool is stopped");
return;
}
new Thread(() -> {
try {
producerThreadMap.put(Thread.currentThread().getId(),Thread.currentThread());
blockingQueue.put(runnable);
} catch (InterruptedException e) {
System.out.println("interrupted exception " + Thread.currentThread().getName());
// e.printStackTrace();
}
// producerThreadMap.remove(Thread.currentThread().getId());
}).start();
}
public synchronized void stop() {
for(int i = 0; i < poolList.size();i++) {
poolList.get(i).stop();
}
System.out.print("size " + producerThreadMap.size());
for(Map.Entry<Long,Thread> entry: producerThreadMap.entrySet()) {
Thread t = entry.getValue();
System.out.println("thread name "+ t.getName());
if(t != null) {
t.interrupt();
}
}
}
public static void main(String[] args) {
ThreadPool pool = new ThreadPool(10,10);
for(int i = 0; i < 100; i++) {
int finalI = i;
int finalI1 = i;
pool.execute(() -> {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("arpit " + Integer.toString(finalI1) );
});
//System.out.println("execution completed");
if( i== 60) {
pool.stop();
}
}
pool.stop();
}
}