resolved joins from main thread as illegal
This commit is contained in:
1 parent
3295ca2af9
commit
2ff7988251
3 files changed
+26
-34
No files matched your search
@@ -12,36 +12,24 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ThreadValue extends Value
|
||||
{
|
||||
private CompletableFuture<Value> taskFuture;
|
||||
private long id;
|
||||
private final Object lock;
|
||||
private final boolean[] started;
|
||||
private Supplier<Value>task;
|
||||
private static long sequence = 0L;
|
||||
|
||||
private static final Map<Value,ThreadPoolExecutor> executorServices = new HashMap<>();
|
||||
|
||||
public ThreadValue(Value pool, FunctionValue function, Expression expr, Tokenizer.Token token, Context ctx)
|
||||
{
|
||||
id = sequence++;
|
||||
lock = new Object();
|
||||
started = new boolean[]{false};
|
||||
task = () ->
|
||||
{
|
||||
if (!started[0])
|
||||
{
|
||||
started[0] = true;
|
||||
return function.lazyEval(ctx, Context.NONE, expr, token, Collections.emptyList()).evalValue(ctx);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
taskFuture = CompletableFuture.supplyAsync(
|
||||
() -> { synchronized (lock) { return task.get(); } },
|
||||
executorServices.computeIfAbsent(pool, (v) -> (ThreadPoolExecutor)Executors.newCachedThreadPool())
|
||||
() -> { synchronized (lock) {
|
||||
return function.lazyEval(ctx, Context.NONE, expr, token, Collections.emptyList()).evalValue(ctx);
|
||||
} },
|
||||
executorServices.computeIfAbsent(pool, (v) -> (ThreadPoolExecutor)Executors.newCachedThreadPool())
|
||||
);
|
||||
Thread.yield();
|
||||
}
|
||||
@@ -60,18 +48,6 @@ public class ThreadValue extends Value
|
||||
|
||||
public Value join()
|
||||
{
|
||||
if (!started[0])
|
||||
{
|
||||
synchronized (lock)
|
||||
{
|
||||
if (!started[0])
|
||||
{
|
||||
Value res = task.get();
|
||||
taskFuture.complete(res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
return taskFuture.get();
|
||||
@@ -93,7 +69,6 @@ public class ThreadValue extends Value
|
||||
{
|
||||
if (!taskFuture.isDone())
|
||||
{
|
||||
started[0] = true;
|
||||
taskFuture.complete(Value.NULL);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user