org.ssssssss.script.parsing.ast.statement.AsyncCall Maven / Gradle / Ivy
The newest version!
package org.ssssssss.script.parsing.ast.statement;
import org.ssssssss.script.compile.MagicScriptCompiler;
import org.ssssssss.script.parsing.Span;
import org.ssssssss.script.parsing.VarIndex;
import org.ssssssss.script.parsing.ast.Expression;
import org.ssssssss.script.runtime.Variables;
import org.ssssssss.script.runtime.function.MagicScriptLambdaFunction;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
/**
* 异步调用
*/
public class AsyncCall extends Expression {
/**
* 默认线程池大小(CPU核心数 * 2)
*/
private static ThreadPoolExecutor threadPoolExecutor = setThreadPoolExecutorSize(Runtime.getRuntime().availableProcessors() * 2);
private final LambdaFunction expression;
public AsyncCall(Span span, Expression expression) {
super(span);
if (expression instanceof LambdaFunction) {
this.expression = (LambdaFunction) expression;
} else {
this.expression = new LambdaFunction(span, Collections.emptyList(), Collections.singletonList(new Return(span, expression)));
}
this.expression.setAsync(true);
}
public static ThreadPoolExecutor setThreadPoolExecutorSize(int size) {
if (size > 0) {
threadPoolExecutor = new ThreadPoolExecutor(size, size, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(size * 2), new AsyncThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
}
return threadPoolExecutor;
}
public static FutureTask