My favorites | Sign in
Project Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
                
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package util;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.RunnableFuture;
import java.util.logging.Logger;
import util.io.IoUtils;

/**
* Warning:
* Some IO tasks don't respond to interrupts.
* A common hang like this is using url.openStream -
* one solution is to set the default timeout properties,
* but that can lead to failures in slow networks.
* Instead, in the ChainRunnable save the HttpURLConnection
* (with a cast from url.openConnection) in a volatile field,
* and in the doCancel(Throwable) method :
* if(conn != null){
* conn.setConnectTimeout(1);
* conn.setReadTimeout(1);
* conn.disconnect();
* }
*
*
* This class methods compose ChainRunnable, and
* have a few Executor factories that have special
* thread creation and survival characteristics.
*
* @author Owner
*/
public final class Threads {

/**
* This future is a null object. Its methods do
* nothing except log and return null (!)
* So if you have a bug, remember this is just
* a initialization (volatile) and testing aid.
* @param <T>
* @return
*/
public static <T> Future<T> newLoggingNullFuture() {
return new FutureTask<T>(new Runnable() {

@Override
public void run() {
Logger.getLogger(Threads.class.getName()).warning("Warning, using null object that probably shouldn't be used");
}
}, null);
}

/**
* This runnable is a null object. Its method does
* nothing except log.
* So if you have a bug, remember this is just
* a initialization (volatile) and testing aid.
* @param <T>
* @return
*/
public static Runnable newLoggingNullRunnable() {
return new Runnable() {

@Override
public void run() {
Logger.getLogger(Threads.class.getName()).warning("Warning, using null object that probably shouldn't be used");
}
};
}

/**
* This future is a null object. Its call method does
* nothing, but it returns the given value.
*/
public static <T> Future<T> newObjectFuture(T value) {
return new FutureTask<T>(new Runnable() {

@Override
public void run() {
}
}, value);
}

private static ChainExecutor createExecutor(boolean shutdownOnExit, int minimumNThreads, int maximumNThreads, long secondsTimeOut, BlockingQueue<Runnable> queue, String name) {
final ChainExecutor executor = new ChainExecutor(
minimumNThreads,
maximumNThreads,
secondsTimeOut,
TimeUnit.SECONDS,
queue,
IoUtils.createThreadFactory(true, "ChainExecutor-" + name));
if (shutdownOnExit) {
IoUtils.addShutdownHook(new Runnable() {

@Override
public void run() {
executor.shutdownNow();
}
});
}
return executor;
}

private Threads() {
}

/**
* A pool with exactly N threads that rejects any task after
* if there are no threads available.
* @throws RejectedExecutionException if a task is submited
* when all threads are busy.
*/
public static ExecutorService newFixedRejectingExecutor(String name, int nThreads, boolean shutdownOnExit) {
ChainExecutor executor = createExecutor(shutdownOnExit, nThreads, nThreads, 0L, new LinkedBlockingQueue<Runnable>(nThreads), name);
return executor;
}

/**
* A pool with exactly N threads that discards any task after
* if there are no threads available.
*/
public static ExecutorService newFixedDiscardingExecutor(String name, int nThreads, boolean shutdownOnExit) {
ChainExecutor executor = createExecutor(shutdownOnExit, nThreads, nThreads, 0L, new LinkedBlockingQueue<Runnable>(nThreads), name);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
return executor;
}

/**
* A pool with exactly nThreads that don't timeout
* @param nThreads > 0
* @param shutdownOnExit run a Shutdown Hook to shutdown on exit.
* @return
*/
public static ExecutorService newFIFOFixedExecutor(String name, int nThreads, boolean shutdownOnExit) {
return createExecutor(shutdownOnExit, nThreads, nThreads, 0L, new LinkedBlockingQueue<Runnable>(), name);
}

/**
* A pool with exactly nThreads that don't timeout
* @param nThreads > 0
* @param shutdownOnExit run a Shutdown Hook to shutdown on exit.
* @return
*/
public static ExecutorService newLIFOFixedExecutor(String name, int nThreads, boolean shutdownOnExit) {
return createExecutor(shutdownOnExit, nThreads, nThreads, 0L, new LifoQueue<Runnable>(), name);
}

/**
* A pool that has no maximum number of threads
* and will kill the threads after a timeout after the last task
* @param secondsTimeOut > 0
* @param shutdownOnExit run a Shutdown Hook to shutdown on exit.
* @return
*/
public static ExecutorService newFIFOCachedExecutor(String name, long secondsTimeout, boolean shutdownOnExit) {
return createExecutor(shutdownOnExit, 0, Integer.MAX_VALUE, secondsTimeout, new SynchronousQueue<Runnable>(), name);
}

/**
* A pool that has a maximum number of threads
* and will kill the threads after a timeout after the last task
* @param maximumNThreads > 0
* @param secondsTimeOut > 0
* @param shutdownOnExit run a Shutdown Hook to shutdown on exit.
* @return
*/
public static ExecutorService newFIFOScalingExecutor(String name, int maximumNThreads, long secondsTimeOut, boolean shutdownOnExit) {
ScalingQueue<Runnable> queue = new ScalingQueue<Runnable>();
ChainExecutor executor = createExecutor(shutdownOnExit, 0, maximumNThreads, secondsTimeOut, queue, name);
executor.setRejectedExecutionHandler(new ForceQueuePolicy());
queue.setThreadPoolExecutor(executor);
return executor;
}

/**
* A pool that has a maximum number of threads
* and will kill the threads after a timeout after the last task
* @param maximumNThreads > 0
* @param secondsTimeOut > 0
* @param shutdownOnExit run a Shutdown Hook to shutdown on exit.
* @return
*/
public static ExecutorService newLIFOScalingExecutor(String name, int maximumNThreads, long secondsTimeOut, boolean shutdownOnExit) {
LifoScalingQueue<Runnable> queue = new LifoScalingQueue<Runnable>();
ChainExecutor executor = createExecutor(shutdownOnExit, 0, maximumNThreads, secondsTimeOut, queue, name);
executor.setRejectedExecutionHandler(new ForceQueuePolicy());
queue.setThreadPoolExecutor(executor);
return executor;
}

/**
* A pool that has a minimum number of threads, a maximum number of threads
* and will kill maximumNThreads - minimumNThreads after a timeout after the last task
* @param minimumNThreads >= 0
* @param maximumNThreads > 0
* @param secondsTimeOut > 0
* @param shutdownOnExit run a Shutdown Hook to shutdown on exit.
* @return
*/
public static ExecutorService newFIFOScalingExecutor(String name, int minimumNThreads, int maximumNThreads, long secondsTimeOut, boolean shutdownOnExit) {
ScalingQueue<Runnable> queue = new ScalingQueue<Runnable>();
ChainExecutor executor = createExecutor(shutdownOnExit, minimumNThreads, maximumNThreads, secondsTimeOut, queue, name);
executor.setRejectedExecutionHandler(new ForceQueuePolicy());
queue.setThreadPoolExecutor(executor);
return executor;
}

/**
* A pool that has a minimum number of threads, a maximum number of threads
* and will kill maximumNThreads - minimumNThreads after a timeout after the last task
* @param minimumNThreads >= 0
* @param maximumNThreads > 0
* @param secondsTimeOut > 0
* @param shutdownOnExit run a Shutdown Hook to shutdown on exit.
* @return
*/
public static ExecutorService newLIFOScalingExecutor(String name, int minimumNThreads, int maximumNThreads, long secondsTimeOut, boolean shutdownOnExit) {
LifoScalingQueue<Runnable> queue = new LifoScalingQueue<Runnable>();
ChainExecutor executor = createExecutor(shutdownOnExit, minimumNThreads, maximumNThreads, secondsTimeOut, queue, name);
executor.setRejectedExecutionHandler(new ForceQueuePolicy());
queue.setThreadPoolExecutor(executor);
return executor;
}

/**
* As you can see, we are going to reject the addition of a new task if
* there are no threads to handle it. This will cause the thread pool executor
* to try and allocate a new thread (up to the maximum threads).
* If there are no threads, the task will be rejected.
* In our case, if the task is rejected, we would like to put it back to the queue.
* This is a simple thing to do with ThreadPoolExecutor since we can implement
* our own RejectedExecutionHandler
*/
private static class ScalingQueue<E> extends LinkedBlockingQueue<E> {

/**
* The executor this Queue belongs to
*/
private ThreadPoolExecutor executor;

/**
* Creates a <tt>TaskQueue</tt> with a capacity of
* {@link Integer#MAX_VALUE}.
*/
public ScalingQueue() {
super();
}

/**
* Creates a <tt>TaskQueue</tt> with the given (fixed) capacity.
*
* @param capacity the capacity of this queue.
*/
public ScalingQueue(int capacity) {
super(capacity);
}

/**
* Sets the executor this queue belongs to.
*/
public void setThreadPoolExecutor(ThreadPoolExecutor executor) {
this.executor = executor;
}

/**
* Inserts the specified element at the tail of this queue if there is at
* least one available thread to run the current task. If all pool threads
* are actively busy, it rejects the offer.
*
* @param o the element to add.
* @return <tt>true</tt> if it was possible to add the element to this
* queue, else <tt>false</tt>
* @see ThreadPoolExecutor#execute(Runnable)
*/
@Override
public boolean offer(E o) {
int allWorkingThreads = executor.getActiveCount() + super.size();
return allWorkingThreads < executor.getPoolSize() && super.offer(o);
}
}

/**
* A queue that acts like a lifo
*/
private static class LifoQueue<E> extends LinkedBlockingDeque<E> {

public LifoQueue(int capacity) {
super(capacity);
}

public LifoQueue() {
super();
}

@Override
public void put(E item) throws InterruptedException {
putFirst(item);
}

@Override
public E take() throws InterruptedException {
return takeFirst();
}

@Override
public boolean offer(E item) {
return offerFirst(item);
}

@Override
public boolean add(E e) {
addFirst(e);
return true;
}

@Override
public E peek() {
return peekFirst();
}

@Override
public E poll() {
return pollFirst();
}

@Override
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
return pollFirst(timeout, unit);
}

@Override
public E pop() {
return removeFirst();
}

@Override
public void push(E item) {
addFirst(item);
}
}

private static class LifoScalingQueue<E> extends LifoQueue<E> {

/**
* The executor this Queue belongs to
*/
private ThreadPoolExecutor executor;

/**
* Creates a <tt>TaskQueue</tt> with a capacity of
* {@link Integer#MAX_VALUE}.
*/
public LifoScalingQueue() {
super();
}

/**
* Creates a <tt>TaskQueue</tt> with the given (fixed) capacity.
*
* @param capacity the capacity of this queue.
*/
public LifoScalingQueue(int capacity) {
super(capacity);
}

/**
* Sets the executor this queue belongs to.
*/
public void setThreadPoolExecutor(ThreadPoolExecutor executor) {
this.executor = executor;
}

/**
* Inserts the specified element at the tail of this queue if there is at
* least one available thread to run the current task. If all pool threads
* are actively busy, it rejects the offer.
*
* @param o the element to add.
* @return <tt>true</tt> if it was possible to add the element to this
* queue, else <tt>false</tt>
* @see ThreadPoolExecutor#execute(Runnable)
*/
@Override
public boolean offer(E o) {
int allWorkingThreads = executor.getActiveCount() + super.size();
return allWorkingThreads < executor.getPoolSize() && super.offer(o);
}
}

private static class ForceQueuePolicy implements RejectedExecutionHandler {

@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
try {
executor.getQueue().put(r);
} catch (InterruptedException e) {
//should never happen since we never wait
throw new RejectedExecutionException(e);
}
}
}

/**
* Link conditional chains. If the first fail,
* the second tries to execute, etc.
* If one succeds the value returned is that
* calculated there. No values are passed between
* chains (they start with their value, default null).
* It will ignore exceptions thrown by the composed
* results. If all are cancelled/throw/return null,
* it returns null.
*/
public static <T> T compose(ChainCallable<T>... composed) {
for (ChainCallable<T> chain : composed) {
try {
T output = chain.call();
if (output != null) {
return output;
}
} catch (Exception t) {
//ignore it, try the next.
}
}
return null;
}

/**
* Creates a chain with a null first arg value
* @param <T> the links arguments
* @param firstArg the first argument
* @param root the first link
* @param links the rest of the links
* @return a complete chain of execution
*/
public static <T> ChainCallable<T> appendChains(ChainCallable<T> root, ChainCallable<T>... links) {
return appendChains(null, root, links);
}

/**
* Creates a chain with a T first arg value
* @param <T> the links arguments
* @param firstArg the first argument
* @param root the first link
* @param links the rest of the links
* @return a complete chain of execution
*/
public static <T> ChainCallable<T> appendChains(T firstArg, ChainCallable<T> root, ChainCallable<T>... links) {
root.arg = firstArg;
ChainCallable<T> last = root;
if (links != null) {
for (int i = 0; i < links.length; i++) {
while (last.inner != null) {
last = last.inner;
}
last.inner = links[i];
last = links[i];
}
}
return root;
}

private static final class ChainExecutor extends ThreadPoolExecutor implements ExecutorService {
//needs to be protected by synchronized
//(including the iterator, and Collections doesn't do that)

private final Object lock = new Object();
private Collection<DelegateFutureTask> tasks = new HashSet<DelegateFutureTask>();

/**
* {@link java.util.concurrent.ThreadPoolExecutor#ThreadPoolExecutor(int corePoolSize,
* int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) superclass contructor passthruu}
*/
public ChainExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}

private final static class DelegateFutureTask<T> extends FutureTask<T> {

private final ChainCallable innerComputation;

public DelegateFutureTask(ChainCallable<T> callable) {
super(callable);
innerComputation = callable;
}

public ChainCallable getInnerComputation() {
return innerComputation;
}
}

/**
* Override so that we can recognize the Chain.
* @param <T>
* @param callable
* @return
*/
@Override
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
if (callable instanceof ChainCallable) {
return new DelegateFutureTask<T>((ChainCallable<T>) callable);
}
return super.newTaskFor(callable);
}

@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (r instanceof DelegateFutureTask) {
synchronized (lock) {
tasks.remove((DelegateFutureTask) r);
}
}
}

@Override
protected void beforeExecute(Thread t, Runnable r) {
if (r instanceof DelegateFutureTask) {
synchronized (lock) {
tasks.add((DelegateFutureTask) r);
}
}

super.beforeExecute(t, r);
}

@Override
public List<Runnable> shutdownNow() {
List<Runnable> unExecuted = super.shutdownNow();
InterruptedException interrupt = new InterruptedException("shutdown interrupt");
synchronized (lock) {
for (DelegateFutureTask c : tasks) {
ChainCallable computation = c.getInnerComputation();
computation.doCancel(interrupt);
}
tasks.clear();
}

return unExecuted;
}
}
}
Show details Hide details

Change log

r161 by i30817 on Today (2 hours ago)   Diff
Refactoring. Fixed a bug with images
processing, and deleted some unused
functions.
Go to: 
Sign in to write a code review

Older revisions

r159 by i30817 on Today (8 hours ago)   Diff
Allow multiple body tags in html
files, don't allow url without a local
file to be saved in the books list
r147 by i30817 on Jan 06, 2010   Diff
New factory methods in Threads. A
small serialization fix.
r145 by i30817 on Jan 05, 2010   Diff
Remove possible exception in wrapper
because of mis-read javadoc
All revisions of this file

File info

Size: 19536 bytes, 558 lines

File properties

svn:mergeinfo
Hosted by Google Code