Mercurial Hosting > luan
diff src/luan/modules/ThreadLuan.java @ 1766:8df0b80e715e
fix scheduled tasks
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 06 Jun 2023 14:33:24 -0600 |
parents | 7c7f28c724e8 |
children | d3ea0380dfb6 |
line wrap: on
line diff
--- a/src/luan/modules/ThreadLuan.java Fri May 26 10:29:55 2023 -0600 +++ b/src/luan/modules/ThreadLuan.java Tue Jun 06 14:33:24 2023 -0600 @@ -1,7 +1,6 @@ package luan.modules; import java.io.Closeable; -import java.util.Arrays; import java.util.Iterator; import java.util.Map; import java.util.HashMap; @@ -26,6 +25,11 @@ public final class ThreadLuan { private static final Logger logger = LoggerFactory.getLogger(ThreadLuan.class); + public static final String CLOSEABLES = "Luan.closeables"; + public interface Closeables { + public void addCloseable(Closeable c) throws LuanException; + } + private static final Executor exec = Executors.newCachedThreadPool(); public static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); @@ -50,8 +54,6 @@ exec.execute(runnable(luan,fn)); } - private static final Map<String,ScheduledFuture> scheduleds = new WeakCacheMap<String,ScheduledFuture>(); - private static void cancel(ScheduledFuture sf,String src) { boolean b = sf.cancel(false); if( !sf.isCancelled() ) @@ -82,17 +84,13 @@ Number delay = Utils.removeNumber(options,"delay"); Number repeatingDelay = Utils.removeNumber(options,"repeating_delay"); Number repeatingRate = Utils.removeNumber(options,"repeating_rate"); - Boolean dontGc = Utils.removeBoolean(options,"dont_gc"); String id = Utils.removeString(options,"id"); + if( id != null ) + logger.error("thread option 'id' is obsolete: "+id); if( repeatingDelay!=null && repeatingRate!=null ) throw new LuanException("can't define both repeating_delay and repeating_rate"); boolean repeating = repeatingDelay!=null || repeatingRate!=null; Utils.checkEmpty(options); - if( id != null ) { - ScheduledFuture sf = scheduleds.remove(id); - if( sf != null ) - cancel(sf,"id "+id); - } final Runnable r = runnable(newLuan,fn); final ScheduledFuture sf; if( repeatingDelay != null ) { @@ -109,16 +107,18 @@ scheduler.schedule(r,0L,TimeUnit.MILLISECONDS); return; } - if( !Boolean.TRUE.equals(dontGc) ) { - Object c = new Object() { + Closeables cs = (Closeables)luan.registry().get(CLOSEABLES); + if( cs != null ) { + Closeable cl = new Closeable() { + public void close() { + cancel(sf,"close"); + } protected void finalize() throws Throwable { - cancel(sf,"gc"); + cancel(sf,"gc"); // cancel on gc } }; - luan.registry().put(c,c); // cancel on gc + cs.addCloseable(cl); } - if( id != null ) - scheduleds.put(id,sf); }