3392d461c2ed05ae6d305f237740ca4d27f05b91
[simantics/platform.git] / bundles / org.simantics.scl.runtime / src / org / simantics / scl / runtime / utils / AsyncUtils.java
1 package org.simantics.scl.runtime.utils;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.util.Arrays;
6 import java.util.List;
7
8 import org.simantics.scl.runtime.SCLContext;
9 import org.simantics.scl.runtime.function.Function;
10 import org.simantics.scl.runtime.reporting.SCLReporting;
11 import org.simantics.scl.runtime.tuple.Tuple0;
12
13 public class AsyncUtils {
14     public static void runAsync(Function f) {
15         SCLContext context = SCLContext.createDerivedContext();
16         new Thread() {
17             @Override
18             public void run() {
19                 SCLContext.push(context);
20                 try {
21                     f.apply(Tuple0.INSTANCE);
22                 } catch(Exception e) {
23                     StringWriter sw = new StringWriter();
24                     e.printStackTrace(new PrintWriter(sw));
25                     SCLReporting.printError(sw.toString());
26                 } finally {
27                     SCLContext.pop();
28                 }
29             }
30         }.start();
31     }
32     
33     public static List<Object> pmap(Function f, List<Object> l) {
34         return Arrays.asList(l.parallelStream().map(a -> f.apply(a)).toArray());
35     }
36 }