Mercurial Hosting > luan
comparison core/src/luan/modules/ThreadLuan.java @ 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 | cdc70de628b5 |
children |
comparison
equal
deleted
inserted
replaced
714:7a322e942c15 | 715:a82d385ec2c3 |
---|---|
1 package luan.modules; | 1 package luan.modules; |
2 | 2 |
3 import java.io.Closeable; | |
3 import java.util.concurrent.Executor; | 4 import java.util.concurrent.Executor; |
4 import java.util.concurrent.Executors; | 5 import java.util.concurrent.Executors; |
6 import java.util.concurrent.ScheduledExecutorService; | |
7 import java.util.concurrent.ScheduledFuture; | |
8 import java.util.concurrent.TimeUnit; | |
5 import luan.Luan; | 9 import luan.Luan; |
6 import luan.LuanState; | 10 import luan.LuanState; |
7 import luan.LuanFunction; | 11 import luan.LuanFunction; |
8 import luan.LuanTable; | 12 import luan.LuanTable; |
9 import luan.LuanException; | 13 import luan.LuanException; |
10 import luan.DeepCloner; | 14 import luan.DeepCloner; |
11 | 15 |
12 | 16 |
13 public final class ThreadLuan { | 17 public final class ThreadLuan { |
14 private static final Executor exec = Executors.newCachedThreadPool(); | 18 private static final Executor exec = Executors.newCachedThreadPool(); |
19 private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); | |
15 | 20 |
16 public static void fork(LuanState luan,LuanFunction fn,Object... args) { | 21 public static void fork(LuanState luan,LuanFunction fn,Object... args) { |
17 DeepCloner cloner = new DeepCloner(); | 22 DeepCloner cloner = new DeepCloner(); |
18 final LuanState newLuan = (LuanState)cloner.deepClone(luan); | 23 final LuanState newLuan = (LuanState)cloner.deepClone(luan); |
19 final LuanFunction newFn = (LuanFunction)cloner.get(fn); | 24 final LuanFunction newFn = (LuanFunction)cloner.get(fn); |
36 } | 41 } |
37 } | 42 } |
38 }; | 43 }; |
39 } | 44 } |
40 | 45 |
46 public static void schedule(LuanState luan,long delay,boolean repeat,LuanFunction fn,Object... args) { | |
47 DeepCloner cloner = new DeepCloner(); | |
48 final LuanState newLuan = (LuanState)cloner.deepClone(luan); | |
49 final LuanFunction newFn = (LuanFunction)cloner.get(fn); | |
50 final Object[] newArgs = cloner.deepClone(args); | |
51 Runnable r = new Runnable(){public void run() { | |
52 try { | |
53 newFn.call(newLuan,newArgs); | |
54 } catch(LuanException e) { | |
55 e.printStackTrace(); | |
56 } | |
57 }}; | |
58 final ScheduledFuture sf; | |
59 if( repeat ) { | |
60 sf = scheduler.scheduleWithFixedDelay(r,delay,delay,TimeUnit.MILLISECONDS); | |
61 } else { | |
62 sf = scheduler.schedule(r,delay,TimeUnit.MILLISECONDS); | |
63 } | |
64 final Closeable c = new Closeable(){public void close(){ | |
65 boolean b = sf.cancel(false); | |
66 }}; | |
67 luan.registry().put(c,c); // prevent gc | |
68 luan.onClose(c); | |
69 } | |
70 | |
41 } | 71 } |