Mercurial Hosting > luan
view src/luan/modules/ThreadLuan.java @ 1098:bae624e455e2
add Time.time_zone
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 29 Mar 2017 19:54:45 -0600 |
parents | 0d884377e923 |
children | a5406f076726 |
line wrap: on
line source
package luan.modules; import java.io.Closeable; import java.util.Map; import java.util.LinkedHashMap; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import luan.Luan; import luan.LuanState; import luan.LuanFunction; import luan.LuanTable; import luan.LuanException; import luan.LuanCloner; public final class ThreadLuan { private static final Executor exec = Executors.newCachedThreadPool(); private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); public static void fork(LuanState luan,LuanFunction fn,Object... args) { LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); final LuanState newLuan = (LuanState)cloner.clone(luan); final LuanFunction newFn = (LuanFunction)cloner.get(fn); final Object[] newArgs = cloner.clone(args); exec.execute(new Runnable(){public void run() { try { newFn.call(newLuan,newArgs); } catch(LuanException e) { e.printStackTrace(); } }}); } /* public static LuanFunction synchronized_(final LuanState luan,final LuanFunction fn) throws LuanException { Utils.checkNotNull(fn); return new LuanFunction() { @Override public Object call(LuanState ingored,Object[] args) throws LuanException { synchronized(luan) { return fn.call(luan,args); } } }; } */ public static void schedule(LuanState luan,long delay,boolean repeat,LuanFunction fn,Object... args) { LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); final LuanState newLuan = (LuanState)cloner.clone(luan); final LuanFunction newFn = (LuanFunction)cloner.get(fn); final Object[] newArgs = cloner.clone(args); Runnable r = new Runnable(){public void run() { try { newFn.call(newLuan,newArgs); } catch(LuanException e) { e.printStackTrace(); } }}; final ScheduledFuture sf; if( repeat ) { sf = scheduler.scheduleWithFixedDelay(r,delay,delay,TimeUnit.MILLISECONDS); } else { sf = scheduler.schedule(r,delay,TimeUnit.MILLISECONDS); } final Closeable c = new Closeable(){public void close(){ boolean b = sf.cancel(false); }}; luan.registry().put(c,c); // prevent gc luan.onClose(c); } public static class StringMap { private static class Value { final long time = System.currentTimeMillis(); final String s; Value(String s) { this.s = s; } } public long timeout = 60000L; // one minute private Map<String,Value> map = new LinkedHashMap<String,Value>() { protected boolean removeEldestEntry(Map.Entry<String,Value> eldest) { return eldest.getValue().time < System.currentTimeMillis() - timeout; } }; public synchronized String get(String key) { Value val = map.get(key); return val==null ? null : val.s; } public synchronized void set(String key,String s) { map.put(key,new Value(s)); } } }