Mercurial Hosting > luan
annotate 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 |
rev | line source |
---|---|
167
4c0131c2b650
merge luan/lib into modules
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
142
diff
changeset
|
1 package luan.modules; |
126
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
2 |
715
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
3 import java.io.Closeable; |
794
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
4 import java.util.Map; |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
5 import java.util.LinkedHashMap; |
126
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
6 import java.util.concurrent.Executor; |
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
7 import java.util.concurrent.Executors; |
715
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
8 import java.util.concurrent.ScheduledExecutorService; |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
9 import java.util.concurrent.ScheduledFuture; |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
10 import java.util.concurrent.TimeUnit; |
221
ec016471c6eb
make LuanTable an interface
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
11 import luan.Luan; |
126
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
12 import luan.LuanState; |
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
13 import luan.LuanFunction; |
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
14 import luan.LuanTable; |
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
15 import luan.LuanException; |
781
fbbdd369a13a
rename DeepCloner to LuanCloner
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
16 import luan.LuanCloner; |
126
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
17 |
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
18 |
168
ebe9db183eb7
rename *Lib.java to *Luan.java
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
167
diff
changeset
|
19 public final class ThreadLuan { |
126
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
20 private static final Executor exec = Executors.newCachedThreadPool(); |
715
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
21 private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
126
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
22 |
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
23 public static void fork(LuanState luan,LuanFunction fn,Object... args) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
782
diff
changeset
|
24 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
25 final LuanState newLuan = (LuanState)cloner.clone(luan); |
433
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
301
diff
changeset
|
26 final LuanFunction newFn = (LuanFunction)cloner.get(fn); |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
27 final Object[] newArgs = cloner.clone(args); |
126
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
28 exec.execute(new Runnable(){public void run() { |
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
29 try { |
574
6cc2f047019b
remove LuanState.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
433
diff
changeset
|
30 newFn.call(newLuan,newArgs); |
126
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
31 } catch(LuanException e) { |
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
32 e.printStackTrace(); |
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
33 } |
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
34 }}); |
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
35 } |
788
c9409e28daa2
remove Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents:
785
diff
changeset
|
36 /* |
582
31926755689e
add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents:
574
diff
changeset
|
37 public static LuanFunction synchronized_(final LuanState luan,final LuanFunction fn) throws LuanException { |
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
582
diff
changeset
|
38 Utils.checkNotNull(fn); |
582
31926755689e
add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents:
574
diff
changeset
|
39 return new LuanFunction() { |
31926755689e
add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents:
574
diff
changeset
|
40 @Override public Object call(LuanState ingored,Object[] args) throws LuanException { |
31926755689e
add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents:
574
diff
changeset
|
41 synchronized(luan) { |
31926755689e
add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents:
574
diff
changeset
|
42 return fn.call(luan,args); |
31926755689e
add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents:
574
diff
changeset
|
43 } |
31926755689e
add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents:
574
diff
changeset
|
44 } |
31926755689e
add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents:
574
diff
changeset
|
45 }; |
31926755689e
add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents:
574
diff
changeset
|
46 } |
788
c9409e28daa2
remove Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents:
785
diff
changeset
|
47 */ |
715
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
48 public static void schedule(LuanState luan,long delay,boolean repeat,LuanFunction fn,Object... args) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
782
diff
changeset
|
49 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
50 final LuanState newLuan = (LuanState)cloner.clone(luan); |
715
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
51 final LuanFunction newFn = (LuanFunction)cloner.get(fn); |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
52 final Object[] newArgs = cloner.clone(args); |
715
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
53 Runnable r = new Runnable(){public void run() { |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
54 try { |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
55 newFn.call(newLuan,newArgs); |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
56 } catch(LuanException e) { |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
57 e.printStackTrace(); |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
58 } |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
59 }}; |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
60 final ScheduledFuture sf; |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
61 if( repeat ) { |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
62 sf = scheduler.scheduleWithFixedDelay(r,delay,delay,TimeUnit.MILLISECONDS); |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
63 } else { |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
64 sf = scheduler.schedule(r,delay,TimeUnit.MILLISECONDS); |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
65 } |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
66 final Closeable c = new Closeable(){public void close(){ |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
67 boolean b = sf.cancel(false); |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
68 }}; |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
69 luan.registry().put(c,c); // prevent gc |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
70 luan.onClose(c); |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
71 } |
a82d385ec2c3
add Thread.schedule() and improve Time.period()
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
72 |
794
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
73 |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
74 public static class StringMap { |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
75 |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
76 private static class Value { |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
77 final long time = System.currentTimeMillis(); |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
78 final String s; |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
79 |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
80 Value(String s) { |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
81 this.s = s; |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
82 } |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
83 } |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
84 |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
85 public long timeout = 60000L; // one minute |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
86 private Map<String,Value> map = new LinkedHashMap<String,Value>() { |
1080 | 87 protected boolean removeEldestEntry(Map.Entry<String,Value> eldest) { |
794
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
88 return eldest.getValue().time < System.currentTimeMillis() - timeout; |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
89 } |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
90 }; |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
91 |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
92 public synchronized String get(String key) { |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
93 Value val = map.get(key); |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
94 return val==null ? null : val.s; |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
95 } |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
96 |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
97 public synchronized void set(String key,String s) { |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
98 map.put(key,new Value(s)); |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
99 } |
f16c77ef0f4f
add Parsers.json_string and Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
788
diff
changeset
|
100 } |
126
0149bdf98fd8
add ThreadLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
101 } |