comparison src/luan/modules/ThreadLuan.java @ 1301:a9cc35c71eb0

go back to Thread.schedule
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 13 Jan 2019 17:14:45 -0700
parents 9fa8b8389578
children 5345466fa61f
comparison
equal deleted inserted replaced
1300:590ad449ac7f 1301:a9cc35c71eb0
46 } 46 }
47 } 47 }
48 }; 48 };
49 } 49 }
50 */ 50 */
51 public static void schedule(LuanState luan,long delay,boolean repeat,LuanFunction fn,Object... args) { 51 public static void schedule(LuanState luan,long delay,LuanFunction fn,String repeating)
52 throws LuanException
53 {
52 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); 54 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
53 final LuanState newLuan = (LuanState)cloner.clone(luan); 55 final LuanState newLuan = (LuanState)cloner.clone(luan);
54 final LuanFunction newFn = (LuanFunction)cloner.get(fn); 56 final LuanFunction newFn = (LuanFunction)cloner.get(fn);
55 final Object[] newArgs = cloner.clone(args);
56 Runnable r = new Runnable(){public void run() { 57 Runnable r = new Runnable(){public void run() {
57 try { 58 try {
58 newFn.call(newLuan,newArgs); 59 newFn.call(newLuan);
59 } catch(LuanException e) { 60 } catch(LuanException e) {
60 e.printStackTrace(); 61 e.printStackTrace();
61 } 62 }
62 }}; 63 }};
63 final ScheduledFuture sf; 64 final ScheduledFuture sf;
64 if( repeat ) { 65 if( repeating==null ) {
66 sf = scheduler.schedule(r,delay,TimeUnit.MILLISECONDS);
67 } else if( repeating.equals("with_fixed_delay") ) {
65 sf = scheduler.scheduleWithFixedDelay(r,delay,delay,TimeUnit.MILLISECONDS); 68 sf = scheduler.scheduleWithFixedDelay(r,delay,delay,TimeUnit.MILLISECONDS);
69 } else if( repeating.equals("at_fixed_rate") ) {
70 sf = scheduler.scheduleAtFixedRate(r,delay,delay,TimeUnit.MILLISECONDS);
66 } else { 71 } else {
67 sf = scheduler.schedule(r,delay,TimeUnit.MILLISECONDS); 72 throw new LuanException("repeat must be nil or 'with_fixed_delay' or 'at_fixed_rate'");
68 } 73 }
69 final Closeable c = new Closeable(){public void close(){ 74 final Closeable c = new Closeable(){public void close(){
70 boolean b = sf.cancel(false); 75 boolean b = sf.cancel(false);
71 }}; 76 }};
72 newLuan.registry().put(luan,luan); // prevent gc 77 newLuan.registry().put(luan,luan); // prevent gc