| Matching Statements |
| File1 Line# |
File2 Line# |
Statement |
| 8 | 7 | package java.util.concurrent |
| 10 | 8 | import java.util.* |
| 26 | 24 | public abstract class AbstractExecutorService implements ExecutorService { |
| 28 | 30 | public Future<?> submit(Runnable task) { |
29 36 43 | 31 42 53 | if (task == null) throw new NullPointerException() |
| 30 | 32 | FutureTask<Object> ftask = new FutureTask<Object>(task, null) |
31 38 45 | 33 44 55 | execute(ftask) |
32 39 46 | 34 45 56 | return ftask |
| 35 | 41 | public <T> Future<T> submit(Runnable task, T result) { |
| 37 | 43 | FutureTask<T> ftask = new FutureTask<T>(task, result) |
| 42 | 52 | public <T> Future<T> submit(Callable<T> task) { |
| 44 | 54 | FutureTask<T> ftask = new FutureTask<T>(task) |
| 52 | 62 | private <T> T doInvokeAny(Collection<Callable<T>> tasks, |
| 53 | 63 | boolean timed, long nanos) |
54 139 | 64 149 | throws InterruptedException, ExecutionException, TimeoutException { |
55 145 | 65 155 | if (tasks == null) |
56 146 177 | 66 156 187 | throw new NullPointerException() |
| 57 | 67 | int ntasks = tasks.size() |
| 58 | 68 | if (ntasks == 0) |
| 59 | 69 | throw new IllegalArgumentException() |
| 60 | 70 | List<Future<T>> futures= new ArrayList<Future<T>>(ntasks) |
| 61 | 71 | ExecutorCompletionService<T> ecs = |
| 62 | 72 | new ExecutorCompletionService<T>(this) |
| 73 | 83 | ExecutionException ee = null |
| 74 | 84 | long lastTime = (timed)? System.nanoTime() : 0 |
| 75 | 85 | Iterator<Callable<T>> it = tasks.iterator() |
78 87 | 88 97 | futures.add(ecs.submit(it.next())) |
79 86 | 89 96 | --ntasks |
| 80 | 90 | int active = 1 |
| 83 | 93 | Future<T> f = ecs.poll() |
| 84 | 94 | if (f == null) { |
| 85 | 95 | if (ntasks > 0) { |
| 88 | 98 | ++active |
| 90 | 100 | else if (active == 0) |
| 92 | 102 | else if (timed) { |
| 93 | 103 | f = ecs.poll(nanos, TimeUnit.NANOSECONDS) |
| 94 | 104 | if (f == null) |
| 95 | 105 | throw new TimeoutException() |
96 192 210 | 106 202 220 | long now = System.nanoTime() |
97 193 211 | 107 203 221 | nanos -= now - lastTime |
98 194 212 | 108 204 222 | lastTime = now |
| 101 | 111 | f = ecs.take() |
| 103 | 113 | if (f != null) { |
| 104 | 114 | --active |
| 106 | 116 | return f.get() |
| 107 | 117 | } catch(InterruptedException ie) { |
| 108 | 118 | throw ie |
| 109 | 119 | } catch(ExecutionException eex) { |
| 110 | 120 | ee = eex |
| 111 | 121 | } catch(RuntimeException rex) { |
| 112 | 122 | ee = new ExecutionException(rex) |
| 117 | 127 | if (ee == null) |
| 118 | 128 | ee = new ExecutionException() |
| 119 | 129 | throw ee |
122 168 219 | 132 178 229 | for (Future<T> f : futures) |
123 169 220 | 133 179 230 | f.cancel(true) |
| 127 | 137 | public <T> T invokeAny(Collection<Callable<T>> tasks) |
| 128 | 138 | throws InterruptedException, ExecutionException { |
| 130 | 140 | return doInvokeAny(tasks, false, 0) |
| 131 | 141 | } catch (TimeoutException cannotHappen) { |
| 132 | 142 | assert false |
| 137 | 147 | public <T> T invokeAny(Collection<Callable<T>> tasks, |
138 174 | 148 184 | long timeout, TimeUnit unit) |
| 140 | 150 | return doInvokeAny(tasks, true, unit.toNanos(timeout)) |
| 143 | 153 | public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks) |
144 175 | 154 185 | throws InterruptedException { |
147 179 | 157 189 | List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size()) |
148 180 | 158 190 | boolean done = false |
| 150 | 160 | for (Callable<T> t : tasks) { |
| 151 | 161 | FutureTask<T> f = new FutureTask<T>(t) |
| 152 | 162 | futures.add(f) |
| 153 | 163 | execute(f) |
155 199 | 165 209 | for (Future<T> f : futures) { |
156 200 | 166 210 | if (!f.isDone()) { |
| 158 | 168 | f.get() |
159 205 | 169 215 | } catch(CancellationException ignore) { |
160 206 | 170 216 | } catch(ExecutionException ignore) { |
164 215 | 174 225 | done = true |
165 196 202 208 216 | 175 206 212 218 226 | return futures |
167 218 | 177 228 | if (!done) |
| 173 | 183 | public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, |
| 176 | 186 | if (tasks == null || unit == null) |
| 178 | 188 | long nanos = unit.toNanos(timeout) |
| 182 | 192 | for (Callable<T> t : tasks) |
| 183 | 193 | futures.add(new FutureTask<T>(t)) |
| 185 | 195 | long lastTime = System.nanoTime() |
| 189 | 199 | Iterator<Future<T>> it = futures.iterator() |
| 190 | 200 | while (it.hasNext()) { |
| 191 | 201 | execute((Runnable)(it.next())) |
195 201 | 205 211 | if (nanos <= 0) |
| 204 | 214 | f.get(nanos, TimeUnit.NANOSECONDS) |
| 207 | 217 | } catch(TimeoutException toe) { |
| Matching Comments and Strings |
| File1 Line# |
File2 Line# |
Comment/String |
| 14 | 12 | * execution methods. This class implements the <tt>submit</tt>, |
| 15 | 13 | * <tt>invokeAny</tt> and <tt>invokeAll</tt> methods using the default |
| 16 | 14 | * {@link FutureTask} class provided in this package. For example, |
| 17 | 15 | * the implementation of <tt>submit(Runnable)</tt> creates an |
| 18 | 16 | * associated <tt>FutureTask</tt> that is executed and |
| 19 | 17 | * returned. Subclasses overriding these methods to use different |
| 20 | 18 | * {@link Future} implementations should do so consistently for each |
| 21 | 19 | * of these methods. |
| 23 | 21 | * @since 1.5 |
| 24 | 22 | * @author Doug Lea |
| 50 | 60 | * the main mechanics of invokeAny. |
| 64 | 74 | For efficiency, especially in executors with limited |
| 65 | 75 | parallelism, check to see if previously submitted tasks are |
| 66 | 76 | done before submitting more of them. This interleaving |
| 67 | 77 | plus the exception mechanics account for messiness of main |
| 68 | 78 | loop. |
| 71 | 81 | Record exceptions so that if we fail to obtain any |
| 72 | 82 | result, we can throw the last exception we got. |
| 77 | 87 | Start one task for sure; the rest incrementally |
| 187 | 197 | Interleave time checks and calls to execute in case |
| 188 | 198 | executor doesn't have any/much parallelism. |