1 package org.simantics.interop.scl;
3 import java.util.concurrent.ScheduledThreadPoolExecutor;
4 import java.util.concurrent.ThreadFactory;
5 import java.util.concurrent.atomic.AtomicInteger;
7 import org.simantics.scl.runtime.SCLContext;
8 import org.simantics.scl.runtime.function.Function;
9 import org.simantics.scl.runtime.tuple.Tuple0;
11 public class Threads {
13 private static final AtomicInteger threadCount = new AtomicInteger(0);
15 private static final ThreadFactory threadFactory = r -> {
16 Thread t = new Thread(r, "scl-interop-thread-" + threadCount.incrementAndGet());
22 private static final ScheduledThreadPoolExecutor scheduledExecutor;
25 scheduledExecutor = new ScheduledThreadPoolExecutor(2, threadFactory);
26 scheduledExecutor.setMaximumPoolSize(2);
29 public static void setMaximumPoolSize(int size) {
32 scheduledExecutor.setMaximumPoolSize(size);
35 @SuppressWarnings({ "rawtypes", "unchecked" })
36 public static SCLThread runAsync(Function f) {
37 SCLContext context = SCLContext.createDerivedContext();
38 SCLThread t = new SCLThread(context,f);
39 scheduledExecutor.submit(t);
43 public static class SCLThread implements Runnable {
46 boolean running = false;
47 boolean completed = false;
48 Object returnValue = null;
51 public SCLThread(SCLContext context, Function f) {
52 this.context = context;
58 SCLContext.push(context);
61 returnValue = f.apply(Tuple0.INSTANCE);
62 } catch (Throwable t) {
71 public boolean isRunning() {
75 public boolean isCompleted() {
79 public boolean hasErrors() {
83 public Object getReturnValue() {
87 public Throwable getError() {