|
18 | 18 | package com.astamuse.asta4d.util.collection; |
19 | 19 |
|
20 | 20 | import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
21 | 22 | import java.util.Collections; |
22 | 23 | import java.util.Iterator; |
23 | 24 | import java.util.LinkedList; |
|
36 | 37 |
|
37 | 38 | import com.astamuse.asta4d.Configuration; |
38 | 39 | import com.astamuse.asta4d.Context; |
39 | | -import com.astamuse.asta4d.util.concurrent.ListExecutorServiceUtil; |
| 40 | +import com.astamuse.asta4d.util.SyncClosureReference; |
40 | 41 |
|
41 | 42 | public class ListConvertUtil { |
42 | 43 |
|
43 | 44 | private final static Logger logger = LoggerFactory.getLogger(ListConvertUtil.class); |
44 | 45 |
|
45 | 46 | private final static String ParallelListConversionMark = "ParallelListConversionMark##" + ListConvertUtil.class.getName(); |
46 | 47 |
|
47 | | - private final static ExecutorService ParallelFallbackExecutor = Executors.newCachedThreadPool(); |
| 48 | + private final static ExecutorService ListExecutorService; |
| 49 | + |
| 50 | + private final static ExecutorService ListDispatchExecutorService; |
| 51 | + |
| 52 | + private final static ExecutorService ParallelFallbackExecutor; |
| 53 | + |
| 54 | + static { |
| 55 | + Configuration conf = Configuration.getConfiguration(); |
| 56 | + ListExecutorService = conf.getParallelListConvertingExecutorFactory().createExecutorService(); |
| 57 | + ListDispatchExecutorService = conf.getParallelListConvertingDispatchExecutorFactory().createExecutorService(); |
| 58 | + ParallelFallbackExecutor = Executors.newCachedThreadPool(); |
| 59 | + } |
48 | 60 |
|
49 | 61 | private final static <S, T> List<T> _transform(Iterable<S> sourceList, RowConvertor<S, T> convertor) { |
50 | 62 | List<T> newList = new LinkedList<>(); |
@@ -125,65 +137,135 @@ public final T get(long timeout, TimeUnit unit) |
125 | 137 | } |
126 | 138 | }); |
127 | 139 | case NEW_THREAD: |
128 | | - ExecutorService executor = ParallelFallbackExecutor; |
129 | | - /* |
130 | | - List<Future<T>> futureList = new LinkedList<>(); |
131 | | - int index = 0; |
132 | | - for (S obj : sourceList) { |
133 | | - futureList.add(invokeByExecutor(executor, convertor, index, obj)); |
134 | | - index++; |
135 | | - } |
136 | | - */ |
137 | | - List<Future<T>> futureList = invokeByExecutor(executor, sourceList, convertor, 2); |
138 | | - return futureList; |
| 140 | + return invokeByExecutor(ParallelFallbackExecutor, sourceList, convertor, 2); |
139 | 141 | default: |
140 | 142 | return Collections.emptyList(); |
141 | 143 | } |
142 | 144 | } else {// in non recursive converting |
143 | 145 | Context newContext = context.clone(); |
144 | 146 | newContext.setData(ParallelListConversionMark, Boolean.TRUE); |
145 | 147 | try { |
146 | | - return Context.with(newContext, new Callable<List<Future<T>>>() { |
147 | | - |
148 | | - @Override |
149 | | - public List<Future<T>> call() throws Exception { |
150 | | - ExecutorService executor = ListExecutorServiceUtil.getExecutorService(); |
151 | | - /* |
152 | | - List<Future<T>> futureList = new LinkedList<>(); |
153 | | - int index = 0; |
154 | | - for (S obj : sourceList) { |
155 | | - futureList.add(invokeByExecutor(executor, convertor, index, obj)); |
156 | | - index++; |
157 | | - } |
158 | | - */ |
159 | | - List<Future<T>> futureList = invokeByExecutor(executor, sourceList, convertor, |
160 | | - conf.getNumberLimitOfParallelListConverting()); |
161 | | - return futureList; |
162 | | - } |
| 148 | + return Context.with(newContext, () -> { |
| 149 | + return dispatchToExecutor(ListDispatchExecutorService, ListExecutorService, sourceList, convertor, |
| 150 | + conf.getNumberLimitOfParallelListConverting()); |
163 | 151 | }); |
164 | 152 | } catch (Exception e) { |
165 | 153 | throw new RuntimeException(e); |
166 | 154 | } |
| 155 | + |
167 | 156 | } // end else in non recursive |
168 | 157 | } |
169 | 158 |
|
170 | | - private static <S, T> Future<T> invokeByExecutor(ExecutorService es, RowConvertor<S, T> convertor, final int rowIndex, final S data) { |
171 | | - final Context context = Context.getCurrentThreadContext(); |
172 | | - return es.submit(new Callable<T>() { |
173 | | - @Override |
174 | | - public T call() throws Exception { |
175 | | - return Context.with(context, new Callable<T>() { |
176 | | - @Override |
177 | | - public T call() throws Exception { |
178 | | - return convertor.convert(rowIndex, data); |
179 | | - } |
180 | | - }); |
| 159 | + private static <S, T> List<Future<T>> dispatchToExecutor(ExecutorService taskDispatchService, ExecutorService taskService, |
| 160 | + Iterable<S> sourceList, RowConvertor<S, T> convertor, int maxParallelNumber) { |
| 161 | + Collection<S> sourceCollection; |
| 162 | + if (sourceList instanceof Collection) { |
| 163 | + sourceCollection = (Collection<S>) sourceList; |
| 164 | + } else { |
| 165 | + sourceCollection = new LinkedList<>(); |
| 166 | + for (S obj : sourceList) { |
| 167 | + sourceCollection.add(obj); |
181 | 168 | } |
| 169 | + } |
| 170 | + |
| 171 | + int size = sourceCollection.size(); |
| 172 | + |
| 173 | + SyncClosureReference<Future<T>[]> futureArrayRef = new SyncClosureReference<>(); |
| 174 | + SyncClosureReference<Exception> exRef = new SyncClosureReference<>(); |
| 175 | + final Context context = Context.getCurrentThreadContext(); |
| 176 | + Future<List<Future<T>>> dispatchResult = taskDispatchService.submit(() -> { |
| 177 | + return Context.with(context, () -> { |
| 178 | + return invokeByExecutor(taskService, sourceCollection, convertor, maxParallelNumber); |
| 179 | + }); |
182 | 180 | }); |
| 181 | + |
| 182 | + List<Future<T>> delegatedList = new LinkedList<>(); |
| 183 | + for (int i = 0; i < size; i++) { |
| 184 | + int idx = i; |
| 185 | + delegatedList.add(new Future<T>() { |
| 186 | + private void waitForDispatched() { |
| 187 | + if (exRef.ref != null || futureArrayRef.ref != null) { |
| 188 | + return; |
| 189 | + } |
| 190 | + // wait for dispatch future |
| 191 | + try { |
| 192 | + List<Future<T>> futureList = dispatchResult.get(); |
| 193 | + @SuppressWarnings("unchecked") |
| 194 | + Future<T>[] fa = new Future[futureList.size()]; |
| 195 | + futureArrayRef.ref = futureList.toArray(fa); |
| 196 | + } catch (InterruptedException | ExecutionException e) { |
| 197 | + exRef.ref = e; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public boolean cancel(boolean mayInterruptIfRunning) { |
| 203 | + if (dispatchResult.cancel(mayInterruptIfRunning)) { |
| 204 | + return true; |
| 205 | + } else { |
| 206 | + waitForDispatched(); |
| 207 | + if (exRef.ref != null) { |
| 208 | + return false; |
| 209 | + } else { |
| 210 | + return futureArrayRef.ref[idx].cancel(mayInterruptIfRunning); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public boolean isCancelled() { |
| 217 | + if (dispatchResult.isCancelled()) { |
| 218 | + return true; |
| 219 | + } else { |
| 220 | + waitForDispatched(); |
| 221 | + if (exRef.ref != null) { |
| 222 | + return false;// it is not so sure |
| 223 | + } else { |
| 224 | + return futureArrayRef.ref[idx].isCancelled(); |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public boolean isDone() { |
| 231 | + if (dispatchResult.isDone()) { |
| 232 | + waitForDispatched(); |
| 233 | + if (exRef.ref != null) { |
| 234 | + return false;// it is not so sure |
| 235 | + } else { |
| 236 | + return futureArrayRef.ref[idx].isDone(); |
| 237 | + } |
| 238 | + } else { |
| 239 | + return false; |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public T get() throws InterruptedException, ExecutionException { |
| 245 | + waitForDispatched(); |
| 246 | + if (exRef.ref != null) { |
| 247 | + throw new ExecutionException(exRef.ref); |
| 248 | + } else { |
| 249 | + return futureArrayRef.ref[idx].get(); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
| 255 | + waitForDispatched(); |
| 256 | + if (exRef.ref != null) { |
| 257 | + throw new ExecutionException(exRef.ref); |
| 258 | + } else { |
| 259 | + return futureArrayRef.ref[idx].get(timeout, unit); |
| 260 | + } |
| 261 | + } |
| 262 | + }); |
| 263 | + } // end for add to list |
| 264 | + return delegatedList; |
183 | 265 | } |
184 | 266 |
|
185 | | - private static <S, T> List<Future<T>> invokeByExecutor(ExecutorService es, Iterable<S> sourceList, RowConvertor<S, T> convertor, |
186 | | - int maxParallelNumber) { |
| 267 | + private static <S, T> List<Future<T>> invokeByExecutor(ExecutorService taskService, Iterable<S> sourceList, |
| 268 | + RowConvertor<S, T> convertor, int maxParallelNumber) { |
187 | 269 | try { |
188 | 270 | final Semaphore available = new Semaphore(maxParallelNumber, false); |
189 | 271 | final Context context = Context.getCurrentThreadContext(); |
@@ -195,7 +277,7 @@ private static <S, T> List<Future<T>> invokeByExecutor(ExecutorService es, Itera |
195 | 277 | // logger.debug("acquiring:{}", rowIndex); |
196 | 278 | available.acquire(); |
197 | 279 | // logger.debug("acquired:{}", rowIndex); |
198 | | - f = es.submit(new Callable<T>() { |
| 280 | + f = taskService.submit(new Callable<T>() { |
199 | 281 | @Override |
200 | 282 | public T call() throws Exception { |
201 | 283 | try { |
|