GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — enhancement: do not block current thread when dispatching tasks in li… · astamuse/asta4d@0c8f253

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • asta4d-core/src/main/java/com/astamuse/asta4d
    • Configuration.java
    • util
      • ClosureReference.java
      • SyncClosureReference.java
      • collection
        • ListConvertUtil.java
      • concurrent
        • ListExecutorServiceUtil.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎asta4d-core/src/main/java/com/astamuse/asta4d/Configuration.java‎

Copy file name to clipboardExpand all lines: asta4d-core/src/main/java/com/astamuse/asta4d/Configuration.java
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public class Configuration {
6767

6868
private ExecutorServiceFactory parallelListConvertingExecutorFactory = new DefaultExecutorServiceFactory("asta4d-parallel-list", 600);
6969

70+
private ExecutorServiceFactory parallelListConvertingDispatchExecutorFactory = new DefaultExecutorServiceFactory(
71+
"asta4d-parallel-list-dispatch", 150);
72+
7073
private boolean blockParallelListRendering = false;
7174

7275
private ParallelRecursivePolicy recursivePolicyForParallelListConverting = ParallelRecursivePolicy.EXCEPTION;
@@ -185,6 +188,14 @@ public void setParallelListConvertingExecutorFactory(ExecutorServiceFactory para
185188
this.parallelListConvertingExecutorFactory = parallelListConvertingExecutorFactory;
186189
}
187190

191+
public ExecutorServiceFactory getParallelListConvertingDispatchExecutorFactory() {
192+
return parallelListConvertingDispatchExecutorFactory;
193+
}
194+
195+
public void setParallelListConvertingDispatchExecutorFactory(ExecutorServiceFactory parallelListConvertingDispatchExecutorFactory) {
196+
this.parallelListConvertingDispatchExecutorFactory = parallelListConvertingDispatchExecutorFactory;
197+
}
198+
188199
public ParallelRecursivePolicy getRecursivePolicyForParallelConverting() {
189200
return recursivePolicyForParallelListConverting;
190201
}
Collapse file

‎asta4d-core/src/main/java/com/astamuse/asta4d/util/ClosureReference.java‎

Copy file name to clipboard
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.astamuse.asta4d.util;
2+
3+
public class ClosureReference<T> {
4+
/**
5+
* This ref is not volatile, which means you should avoid to use it in crossing threads sharing.<br>
6+
* For cross thread sharing, use {@link SyncClosureReference} instead.
7+
*/
8+
public T ref = null;
9+
}
Collapse file

‎asta4d-core/src/main/java/com/astamuse/asta4d/util/SyncClosureReference.java‎

Copy file name to clipboard
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.astamuse.asta4d.util;
2+
3+
public class SyncClosureReference<T> {
4+
/**
5+
* This ref is volatile, which is necessary for make sure all threads can retrieve the reference after it has been assigned. Further,
6+
* since we will write it once and read it many times in most situations, the cost is payable.
7+
*/
8+
public volatile T ref = null;
9+
}
Collapse file

‎asta4d-core/src/main/java/com/astamuse/asta4d/util/collection/ListConvertUtil.java‎

Copy file name to clipboardExpand all lines: asta4d-core/src/main/java/com/astamuse/asta4d/util/collection/ListConvertUtil.java
+126-44Lines changed: 126 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.astamuse.asta4d.util.collection;
1919

2020
import java.util.ArrayList;
21+
import java.util.Collection;
2122
import java.util.Collections;
2223
import java.util.Iterator;
2324
import java.util.LinkedList;
@@ -36,15 +37,26 @@
3637

3738
import com.astamuse.asta4d.Configuration;
3839
import com.astamuse.asta4d.Context;
39-
import com.astamuse.asta4d.util.concurrent.ListExecutorServiceUtil;
40+
import com.astamuse.asta4d.util.SyncClosureReference;
4041

4142
public class ListConvertUtil {
4243

4344
private final static Logger logger = LoggerFactory.getLogger(ListConvertUtil.class);
4445

4546
private final static String ParallelListConversionMark = "ParallelListConversionMark##" + ListConvertUtil.class.getName();
4647

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+
}
4860

4961
private final static <S, T> List<T> _transform(Iterable<S> sourceList, RowConvertor<S, T> convertor) {
5062
List<T> newList = new LinkedList<>();
@@ -125,65 +137,135 @@ public final T get(long timeout, TimeUnit unit)
125137
}
126138
});
127139
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);
139141
default:
140142
return Collections.emptyList();
141143
}
142144
} else {// in non recursive converting
143145
Context newContext = context.clone();
144146
newContext.setData(ParallelListConversionMark, Boolean.TRUE);
145147
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());
163151
});
164152
} catch (Exception e) {
165153
throw new RuntimeException(e);
166154
}
155+
167156
} // end else in non recursive
168157
}
169158

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);
181168
}
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+
});
182180
});
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;
183265
}
184266

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) {
187269
try {
188270
final Semaphore available = new Semaphore(maxParallelNumber, false);
189271
final Context context = Context.getCurrentThreadContext();
@@ -195,7 +277,7 @@ private static <S, T> List<Future<T>> invokeByExecutor(ExecutorService es, Itera
195277
// logger.debug("acquiring:{}", rowIndex);
196278
available.acquire();
197279
// logger.debug("acquired:{}", rowIndex);
198-
f = es.submit(new Callable<T>() {
280+
f = taskService.submit(new Callable<T>() {
199281
@Override
200282
public T call() throws Exception {
201283
try {
Collapse file

‎asta4d-core/src/main/java/com/astamuse/asta4d/util/concurrent/ListExecutorServiceUtil.java‎

Copy file name to clipboardExpand all lines: asta4d-core/src/main/java/com/astamuse/asta4d/util/concurrent/ListExecutorServiceUtil.java
-15Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@
1616
*/
1717
package com.astamuse.asta4d.util.concurrent;
1818

19-
import java.util.concurrent.ExecutorService;
20-
21-
import com.astamuse.asta4d.Configuration;
22-
2319
public class ListExecutorServiceUtil {
2420

25-
private final static ExecutorService listExecutorService;
26-
27-
static {
28-
Configuration conf = Configuration.getConfiguration();
29-
listExecutorService = conf.getParallelListConvertingExecutorFactory().createExecutorService();
30-
}
31-
32-
public final static ExecutorService getExecutorService() {
33-
return listExecutorService;
34-
}
35-
3621
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.