487 |
|
} |
488 |
|
} |
489 |
|
|
490 |
+ |
private static class DequeuableFutureTask<V> extends FutureTask<V> { |
491 |
+ |
private final ThreadPoolExecutor tpe; |
492 |
+ |
public DequeuableFutureTask(Callable<V> callable, ThreadPoolExecutor tpe) { |
493 |
+ |
super(callable); |
494 |
+ |
this.tpe = tpe; |
495 |
+ |
} |
496 |
+ |
|
497 |
+ |
public DequeuableFutureTask(final Runnable runnable, final V result, ThreadPoolExecutor tpe) { |
498 |
+ |
super(runnable, result); |
499 |
+ |
this.tpe = tpe; |
500 |
+ |
} |
501 |
+ |
|
502 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
503 |
+ |
if (!isDone()) |
504 |
+ |
tpe.dequeue(this); |
505 |
+ |
return super.cancel(mayInterruptIfRunning); |
506 |
+ |
} |
507 |
+ |
} |
508 |
+ |
|
509 |
|
/** |
510 |
|
* Creates a new <tt>ThreadPoolExecutor</tt> with the given initial |
511 |
|
* parameters. It may be more convenient to use one of the factory |
687 |
|
// else retry |
688 |
|
} |
689 |
|
} |
690 |
+ |
|
691 |
+ |
public <T> FutureTask<T> executeAsFuture(Callable<T> task) { |
692 |
+ |
FutureTask<T> future = new DequeuableFutureTask<T>(task, this); |
693 |
+ |
execute(future); |
694 |
+ |
return future; |
695 |
+ |
} |
696 |
+ |
|
697 |
+ |
public <T> FutureTask<T> executeAsFuture(Runnable task, T returnValue) { |
698 |
+ |
FutureTask<T> future = new DequeuableFutureTask<T>(task, returnValue, this); |
699 |
+ |
execute(future); |
700 |
+ |
return future; |
701 |
+ |
} |
702 |
+ |
|
703 |
+ |
|
704 |
|
|
705 |
|
public void shutdown() { |
706 |
|
mainLock.lock(); |
796 |
|
} |
797 |
|
|
798 |
|
/** |
799 |
+ |
* Removes this task from internal queue if it is present, thus |
800 |
+ |
* causing it not to be run if it has not already started. This |
801 |
+ |
* method may be useful as one part of a cancellation scheme. |
802 |
+ |
* |
803 |
+ |
* #return true if the task was removed |
804 |
+ |
*/ |
805 |
+ |
public boolean dequeue(Runnable task) { |
806 |
+ |
return getQueue().remove(task); |
807 |
+ |
} |
808 |
+ |
|
809 |
+ |
|
810 |
+ |
/** |
811 |
|
* Sets the core number of threads. This overrides any value set |
812 |
|
* in the constructor. If the new value is smaller than the |
813 |
|
* current value, excess existing threads will be terminated when |