-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneralThreadService.java
214 lines (190 loc) · 6.73 KB
/
GeneralThreadService.java
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.io.*;
import com.google.common.collect.ImmutableList;
/**
* This singleton runs local work units.
*/
public class GeneralThreadService implements Singleton
{
/** The thread pool size. */
public static final int NUMBER_OF_THREADS = Settings.NUMBER_OF_THREADS;
/** How many jobs can wait in the queue at one time. */
public static final int JOB_CAPACITY = 10000000;
private static final CustomThreadPoolExecutor EXECUTOR_SERVICE;
public static final AtomicInteger numberRunning = new AtomicInteger();
/** Not instantiable. */
private GeneralThreadService()
{
throw new IllegalArgumentException("not instantiable");
}
/** Static initializer. */
static
{
// create a thread pool that has exactly NUMBER_OF_THREADS threads
// when started, the executor service will create threads up to NUMBER_OF_THREADS
// these threads will be kept running until the service is shut down
// if more than JOB_CAPACITY jobs is placed in the queue, then the rejected execution policy will decide what happens
// the "caller runs policy" returns the excess work to the calling thread
// threads only created on demand
EXECUTOR_SERVICE = new CustomThreadPoolExecutor(NUMBER_OF_THREADS, // core pool size
NUMBER_OF_THREADS, // maximum pool size
1L, // keep alive time
TimeUnit.MINUTES, // keep alive time unit
new ArrayBlockingQueue<Runnable>(JOB_CAPACITY,true), // work queue
new CustomThreadFactory("thread pool"), // thread factory
new ThreadPoolExecutor.CallerRunsPolicy()); // rejected execution policy
System.out.println("GeneralThreadService initialized with " + NUMBER_OF_THREADS + " threads.");
}
/**
* Forces the static initializer to run.
*/
public static void initialize()
{
}
/**
* Returns the number of jobs that are waiting in the thread pool queue.
* @return the number of waiting jobs
*/
public static int queueSize()
{
return EXECUTOR_SERVICE.getQueue().size();
}
/**
* Submits the specified job.
* @param u the work to run
* @return a promise for the result in the future
*/
public static Future<Result> submit(WorkUnit u)
{
return EXECUTOR_SERVICE.submit(u);
}
/** Alias method. */
public static Future<RemoteResult> submit(RemoteWorkUnit u)
{
return EXECUTOR_SERVICE.submit(u);
}
/**
* The thread pool that will run the work on the local node.
*/
private static class CustomThreadPoolExecutor extends ThreadPoolExecutor
{
public CustomThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
ArrayBlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
{
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
}
protected void beforeExecute(Thread t, Runnable r)
{
super.beforeExecute(t,r);
numberRunning.getAndIncrement();
}
protected void afterExecute(Runnable r, Throwable t)
{
super.afterExecute(r,t);
numberRunning.getAndDecrement();
// print exceptions if any
try
{
Future<?> future = (Future<?>)r;
future.get();
}
catch (ExecutionException e)
{
System.out.println("\n=== EXECUTION EXCEPTION ===\n");
e.getCause().printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* Creates the worker threads that will perform the work.
*/
private static class CustomThreadFactory implements ThreadFactory
{
private final String poolName;
public CustomThreadFactory(String poolName)
{
this.poolName = poolName;
}
// instead of creating Threads, create WorkerThreads (a descendent of Thread)
public Thread newThread(Runnable runnable)
{
return new WorkerThread(runnable, poolName);
}
}
/**
* The worker threads that will actually do the work.
*/
public static class WorkerThread extends Thread
{
private static final AtomicInteger created = new AtomicInteger(); // thread safe integer
public WorkerThread(Runnable runnable, String name)
{
super(runnable, "thread " + created.incrementAndGet());
setDaemon(true);
}
public void run()
{
super.run();
}
}
/**
* Waits for the specified time.
* @param time time in milliseconds to wait
*/
public static void wait(int time)
{
try
{
Thread.sleep(time);
}
catch (InterruptedException e)
{
}
}
/**
* Waits for the specified jobs to finish. No progress report.
* @param futures the futures of the jobs we want to wait for
*/
public static void silentWaitForFutures(List<Future<Result>> futures)
{
int totalJobs = futures.size();
if ( totalJobs == 0 )
return;
while (true)
{
int numberDone = 0;
for (Future<Result> f : futures)
if ( f.isDone() )
numberDone++;
if ( numberDone == totalJobs )
break;
wait(50);
}
}
/**
* Waits for the specified local jobs to finish. Progress report given.
*/
public static void waitForFutures(List<Future<Result>> futures)
{
int totalJobs = futures.size();
if ( totalJobs == 0 )
return;
while (true)
{
int numberDone = 0;
for (Future<Result> f : futures)
if ( f.isDone() )
numberDone++;
System.out.printf("%d of %d jobs complete queue: %d \r", numberDone, totalJobs, queueSize());
if ( numberDone == totalJobs )
break;
wait(50);
}
}
}