Mercurial Hosting > luan
view src/luan/lib/ThreadLib.java @ 135:3119326260ea
revert rev 125 and only preload Java
git-svn-id: https://luan-java.googlecode.com/svn/trunk@136 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 11 Jun 2014 06:30:46 +0000 |
parents | 0594c132888b |
children | 05f8c21160ef |
line wrap: on
line source
package luan.lib; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import luan.LuanState; import luan.LuanFunction; import luan.LuanTable; import luan.LuanJavaFunction; import luan.LuanException; import luan.DeepCloner; public final class ThreadLib { public static final String NAME = "Thread"; public static final LuanFunction LOADER = new LuanFunction() { @Override public Object call(LuanState luan,Object[] args) { LuanTable module = new LuanTable(); try { add( module, "fork", LuanState.class, LuanFunction.class, new Object[0].getClass() ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } return module; } }; private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { t.put( method, new LuanJavaFunction(ThreadLib.class.getMethod(method,parameterTypes),null) ); } private static final Executor exec = Executors.newCachedThreadPool(); public static void fork(LuanState luan,LuanFunction fn,Object... args) { DeepCloner cloner = new DeepCloner(); final LuanState newLuan = cloner.deepClone(luan); final LuanFunction newFn = cloner.get(fn); final Object[] newArgs = cloner.deepClone(args); exec.execute(new Runnable(){public void run() { try { newLuan.call(newFn,"<forked>",newArgs); } catch(LuanException e) { e.printStackTrace(); } }}); } }