Mercurial Hosting > luan
changeset 715:a82d385ec2c3
add Thread.schedule() and improve Time.period()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 23 May 2016 16:16:03 -0600 |
parents | 7a322e942c15 |
children | 28fedb32ab19 |
files | core/src/luan/modules/Thread.luan core/src/luan/modules/ThreadLuan.java core/src/luan/modules/Time.luan |
diffstat | 3 files changed, 35 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
diff -r 7a322e942c15 -r a82d385ec2c3 core/src/luan/modules/Thread.luan --- a/core/src/luan/modules/Thread.luan Fri May 20 18:35:20 2016 -0600 +++ b/core/src/luan/modules/Thread.luan Mon May 23 16:16:03 2016 -0600 @@ -4,6 +4,7 @@ local M = {} M.fork = ThreadLuan.fork +M.schedule = ThreadLuan.schedule M.synchronized = ThreadLuan.synchronized_ return M
diff -r 7a322e942c15 -r a82d385ec2c3 core/src/luan/modules/ThreadLuan.java --- a/core/src/luan/modules/ThreadLuan.java Fri May 20 18:35:20 2016 -0600 +++ b/core/src/luan/modules/ThreadLuan.java Mon May 23 16:16:03 2016 -0600 @@ -1,7 +1,11 @@ package luan.modules; +import java.io.Closeable; 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; @@ -12,6 +16,7 @@ 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) { DeepCloner cloner = new DeepCloner(); @@ -38,4 +43,29 @@ }; } + public static void schedule(LuanState luan,long delay,boolean repeat,LuanFunction fn,Object... args) { + DeepCloner cloner = new DeepCloner(); + final LuanState newLuan = (LuanState)cloner.deepClone(luan); + final LuanFunction newFn = (LuanFunction)cloner.get(fn); + final Object[] newArgs = cloner.deepClone(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); + } + }
diff -r 7a322e942c15 -r a82d385ec2c3 core/src/luan/modules/Time.luan --- a/core/src/luan/modules/Time.luan Fri May 20 18:35:20 2016 -0600 +++ b/core/src/luan/modules/Time.luan Mon May 23 16:16:03 2016 -0600 @@ -54,12 +54,13 @@ return cal.getTimeInMillis() end -function M.period( days, hours, minutes, seconds, millis ) +function M.period( t ) local cal = Calendar.getInstance() cal.setTimeZone(TimeZone.getTimeZone("GMT")) + local days = t.days or 0 days = days + 1 - cal.set( 1970, 0, days, hours or 0, minutes or 0, seconds or 0 ) - cal.set( Calendar.MILLISECOND, millis or 0 ) + cal.set( 1970, 0, days, t.hours or 0, t.minutes or 0, t.seconds or 0 ) + cal.set( Calendar.MILLISECOND, t.millis or 0 ) return cal.getTimeInMillis() end