|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| PollingTaskController.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK* | |
| 2 | ||
| 3 | PLT Utilities BSD License | |
| 4 | ||
| 5 | Copyright (c) 2007-2010 JavaPLT group at Rice University | |
| 6 | All rights reserved. | |
| 7 | ||
| 8 | Developed by: Java Programming Languages Team | |
| 9 | Rice University | |
| 10 | http://www.cs.rice.edu/~javaplt/ | |
| 11 | ||
| 12 | Redistribution and use in source and binary forms, with or without modification, are permitted | |
| 13 | provided that the following conditions are met: | |
| 14 | ||
| 15 | - Redistributions of source code must retain the above copyright notice, this list of conditions | |
| 16 | and the following disclaimer. | |
| 17 | - Redistributions in binary form must reproduce the above copyright notice, this list of | |
| 18 | conditions and the following disclaimer in the documentation and/or other materials provided | |
| 19 | with the distribution. | |
| 20 | - Neither the name of the JavaPLT group, Rice University, nor the names of the library's | |
| 21 | contributors may be used to endorse or promote products derived from this software without | |
| 22 | specific prior written permission. | |
| 23 | ||
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
| 25 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 26 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND | |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
| 30 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 32 | ||
| 33 | *END_COPYRIGHT_BLOCK*/ | |
| 34 | ||
| 35 | package edu.rice.cs.plt.concurrent; | |
| 36 | ||
| 37 | import java.util.concurrent.ExecutionException; | |
| 38 | import java.util.concurrent.TimeUnit; | |
| 39 | import java.util.concurrent.TimeoutException; | |
| 40 | ||
| 41 | import edu.rice.cs.plt.collect.ListenerSet; | |
| 42 | import edu.rice.cs.plt.lambda.LazyRunnable; | |
| 43 | ||
| 44 | /** | |
| 45 | * <p>A TaskController for tasks that are run without any facility for executing code when the task | |
| 46 | * is complete — instead, the controller must either block, poll for the task's current status. | |
| 47 | * When most of the controller's methods are invoked, the current status is polled before proceeding; | |
| 48 | * operations depending on completion block ({@link #finishListeners()} are handled by blocking in a new | |
| 49 | * daemon thread).</p> | |
| 50 | * | |
| 51 | * <p>To implement a concrete instance, a subclass must provide {@link #doStart}, {@link #doStop}, | |
| 52 | * {@link #update}, {@link #finish}, and, optionally, {@link #discard}.</p> | |
| 53 | */ | |
| 54 | public abstract class PollingTaskController<R> extends TaskController<R> { | |
| 55 | ||
| 56 | private final LazyRunnable _startDaemon; | |
| 57 | ||
| 58 | 0 | protected PollingTaskController() { |
| 59 | // a daemon thread is started only if necessary -- if there are no listeners, we can just wait | |
| 60 | // for the user to check on the current status | |
| 61 | 0 | _startDaemon = new LazyRunnable(new Runnable() { |
| 62 | 0 | public void run() { |
| 63 | 0 | Thread t = new Thread("PollingTaskController daemon") { |
| 64 | 0 | public void run() { |
| 65 | 0 | try { finish(); } |
| 66 | catch (InterruptedException e) { /* if interrupted, just stop */ } | |
| 67 | } | |
| 68 | }; | |
| 69 | 0 | t.setDaemon(true); |
| 70 | 0 | t.start(); |
| 71 | } | |
| 72 | }); | |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * Access the ListenerSet responding to the completion of computation. The first time this method | |
| 77 | * is invoked, a daemon thread is started that will wait for computation to complete (otherwise, | |
| 78 | * any registered listeners will not run until the task is manually polled). | |
| 79 | */ | |
| 80 | 0 | public ListenerSet<R>.Sink finishListeners() { |
| 81 | 0 | _startDaemon.run(); |
| 82 | 0 | return super.finishListeners(); |
| 83 | } | |
| 84 | ||
| 85 | ||
| 86 | ||
| 87 | /** | |
| 88 | * Check the current status and call the appropriate method if the task is complete. This method | |
| 89 | * is invoked frequently and should not block. | |
| 90 | */ | |
| 91 | protected abstract void update(); | |
| 92 | ||
| 93 | /** | |
| 94 | * Block until the task is complete or this thread is interrupted. Must not return until after one | |
| 95 | * of the completion methods are called. | |
| 96 | */ | |
| 97 | protected abstract void finish() throws InterruptedException; | |
| 98 | ||
| 99 | /** | |
| 100 | * Block until the task is complete, this thread is interrupted, or the given timeout is reached. Must | |
| 101 | * not return until after one of the completion methods are called. | |
| 102 | */ | |
| 103 | protected abstract void finish(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException; | |
| 104 | ||
| 105 | 0 | protected RunningState runningState() { return new PollingRunningState(); } |
| 106 | ||
| 107 | /** | |
| 108 | * RunningState variant based on a PollingRunner. Extends RunningState to allow it to be | |
| 109 | * recognized as that type. | |
| 110 | */ | |
| 111 | protected class PollingRunningState extends RunningState { | |
| 112 | 0 | public Status status() { |
| 113 | 0 | update(); |
| 114 | 0 | return (state.get() == this) ? Status.RUNNING : state.get().status(); |
| 115 | } | |
| 116 | 0 | public void start() { |
| 117 | 0 | update(); |
| 118 | 0 | if (state.get() != this) { state.get().start(); } |
| 119 | } | |
| 120 | 0 | public R get() throws InterruptedException, ExecutionException { |
| 121 | 0 | finish(); |
| 122 | 0 | if (!isDone()) { throw new IllegalStateException("PollingTaskController returned without finishing"); } |
| 123 | 0 | return state.get().get(); |
| 124 | } | |
| 125 | 0 | public R get(long timeout, TimeUnit u) throws InterruptedException, ExecutionException, TimeoutException { |
| 126 | 0 | finish(timeout, u); |
| 127 | 0 | if (!isDone()) { throw new IllegalStateException("PollingTaskController returned without finishing"); } |
| 128 | 0 | return state.get().get(); |
| 129 | } | |
| 130 | 0 | public boolean cancel(boolean stopRunning) { |
| 131 | 0 | update(); |
| 132 | 0 | if (state.get() == this) { |
| 133 | 0 | if (stopRunning) { |
| 134 | 0 | if (state.compareAndSet(this, new PollingCancelingState())) { doStop(); return true; } |
| 135 | 0 | else { return state.get().cancel(stopRunning); } |
| 136 | } | |
| 137 | 0 | else { return false; } |
| 138 | } | |
| 139 | 0 | else { return state.get().cancel(stopRunning); } |
| 140 | } | |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * CancelingState variant based on a PollingRunner. Extends Canceling to allow it to be | |
| 145 | * recognized as that type. (A lot of code is duplicated from PollingRunningState, but that | |
| 146 | * seems inevitable without overhauling the design to use interfaces to identify different | |
| 147 | * kinds of states.) | |
| 148 | */ | |
| 149 | protected class PollingCancelingState extends CancelingState { | |
| 150 | 0 | public void start() { |
| 151 | 0 | update(); |
| 152 | 0 | if (state.get() != this) { state.get().start(); } |
| 153 | } | |
| 154 | 0 | public Status status() { |
| 155 | 0 | update(); |
| 156 | 0 | return (state.get() == this) ? Status.RUNNING : state.get().status(); |
| 157 | } | |
| 158 | 0 | public boolean cancel(boolean stopRunning) { |
| 159 | 0 | update(); |
| 160 | 0 | return (state.get() == this) ? stopRunning : state.get().cancel(stopRunning); |
| 161 | } | |
| 162 | 0 | public R get() throws InterruptedException, ExecutionException { |
| 163 | 0 | finish(); |
| 164 | 0 | if (!isDone()) { throw new IllegalStateException("PollingRunner returned without finishing"); } |
| 165 | 0 | return state.get().get(); |
| 166 | } | |
| 167 | 0 | public R get(long timeout, TimeUnit u) throws InterruptedException, ExecutionException, TimeoutException { |
| 168 | 0 | finish(timeout, u); |
| 169 | 0 | if (!isDone()) { throw new IllegalStateException("PollingRunner returned without finishing"); } |
| 170 | 0 | return state.get().get(); |
| 171 | } | |
| 172 | } | |
| 173 | ||
| 174 | } |
|
||||||||||