af5c29e9f4fa981bef838b1d007be9c162212f05
[jalviewjs.git] / site / j2s / java / lang / Thread.js
1 Clazz.declarePackage ("java.lang");\r
2 Clazz.load (["java.lang.Enum"], "java.lang.Thread", ["java.lang.Exception", "$.IllegalArgumentException", "$.IllegalThreadStateException", "$.NoSuchMethodError", "$.NullPointerException", "$.ThreadDeath", "java.util.HashMap", "java.lang.ThreadGroup", "swingjs.JSToolkit"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.name = null;\r
5 this.priority = 5;\r
6 this.daemon = false;\r
7 this.target = null;\r
8 this.group = null;\r
9 this.tid = 0;\r
10 this.threadStatus = 0;\r
11 this.parkBlocker = null;\r
12 this.stopBeforeStart = false;\r
13 this.throwableFromStop = null;\r
14 this.me = null;\r
15 this.uncaughtExceptionHandler = null;\r
16 this.$interrupted = false;\r
17 this.stopped = false;\r
18 Clazz.instantialize (this, arguments);\r
19 }, java.lang, "Thread", null, Runnable);\r
20 c$.nextThreadNum = Clazz.defineMethod (c$, "nextThreadNum", \r
21  function () {\r
22 return java.lang.Thread.threadInitNumber++;\r
23 });\r
24 c$.nextThreadID = Clazz.defineMethod (c$, "nextThreadID", \r
25  function () {\r
26 return ++java.lang.Thread.threadSeqNumber;\r
27 });\r
28 c$.currentThread = Clazz.defineMethod (c$, "currentThread", \r
29 function () {\r
30 if (java.lang.Thread.thisThread == null) {\r
31 java.lang.Thread.thisThread =  new java.lang.Thread ("master");\r
32 java.lang.Thread.thisThread.setPriority (5);\r
33 }return java.lang.Thread.thisThread;\r
34 });\r
35 c$.yield = Clazz.defineMethod (c$, "yield", \r
36 function () {\r
37 });\r
38 c$.sleep = Clazz.defineMethod (c$, "sleep", \r
39 function (millis) {\r
40 }, "~N");\r
41 c$.sleep = Clazz.defineMethod (c$, "sleep", \r
42 function (millis, nanos) {\r
43 if (millis < 0) {\r
44 throw  new IllegalArgumentException ("timeout value is negative");\r
45 }if (nanos < 0 || nanos > 999999) {\r
46 throw  new IllegalArgumentException ("nanosecond timeout value out of range");\r
47 }if (nanos >= 500000 || (nanos != 0 && millis == 0)) {\r
48 millis++;\r
49 }java.lang.Thread.sleep (millis);\r
50 }, "~N,~N");\r
51 Clazz.defineMethod (c$, "init", \r
52  function (g, target, name, stackSize) {\r
53 this.init (g, target, name, stackSize, null);\r
54 }, "java.lang.ThreadGroup,Runnable,~S,~N");\r
55 Clazz.defineMethod (c$, "init", \r
56  function (g, target, name, stackSize, acc) {\r
57 var parent = (java.lang.Thread.thisThread == null ? null : java.lang.Thread.thisThread);\r
58 if (g == null) {\r
59 if (g == null && parent != null) {\r
60 g = parent.getThreadGroup ();\r
61 }}if (g == null) {\r
62 g = this.newThreadGroup (null, name);\r
63 parent = this;\r
64 }g.checkAccess ();\r
65 g.addUnstarted ();\r
66 this.group = g;\r
67 this.priority = parent.getPriority ();\r
68 this.name = name;\r
69 this.target = target;\r
70 this.setPriority (this.priority);\r
71 this.tid = java.lang.Thread.nextThreadID ();\r
72 this.me = this;\r
73 }, "java.lang.ThreadGroup,Runnable,~S,~N,~O");\r
74 Clazz.defineMethod (c$, "newThreadGroup", \r
75 function (group, name) {\r
76 return  new java.lang.ThreadGroup (group, name);\r
77 }, "java.lang.ThreadGroup,~S");\r
78 Clazz.makeConstructor (c$, \r
79 function () {\r
80 this.init (null, null, "Thread-" + java.lang.Thread.nextThreadNum (), 0);\r
81 });\r
82 Clazz.makeConstructor (c$, \r
83 function (target) {\r
84 this.init (null, target, "Thread-" + java.lang.Thread.nextThreadNum (), 0);\r
85 }, "Runnable");\r
86 Clazz.makeConstructor (c$, \r
87 function (group, target) {\r
88 this.init (group, target, "Thread-" + java.lang.Thread.nextThreadNum (), 0);\r
89 }, "java.lang.ThreadGroup,Runnable");\r
90 Clazz.makeConstructor (c$, \r
91 function (name) {\r
92 this.init (null, null, name, 0);\r
93 }, "~S");\r
94 Clazz.makeConstructor (c$, \r
95 function (group, name) {\r
96 this.init (group, null, name, 0);\r
97 }, "java.lang.ThreadGroup,~S");\r
98 Clazz.makeConstructor (c$, \r
99 function (target, name) {\r
100 this.init (null, target, name, 0);\r
101 }, "Runnable,~S");\r
102 Clazz.makeConstructor (c$, \r
103 function (group, target, name) {\r
104 this.init (group, target, name, 0);\r
105 }, "java.lang.ThreadGroup,Runnable,~S");\r
106 Clazz.makeConstructor (c$, \r
107 function (group, target, name, stackSize) {\r
108 this.init (group, target, name, stackSize);\r
109 }, "java.lang.ThreadGroup,Runnable,~S,~N");\r
110 Clazz.defineMethod (c$, "start", \r
111 function () {\r
112 if (this.threadStatus != 0 || this !== this.me) throw  new IllegalThreadStateException ();\r
113 this.group.add (this);\r
114 this.start0 ();\r
115 if (this.stopBeforeStart) {\r
116 this.stop0 (this.throwableFromStop);\r
117 }});\r
118 Clazz.defineMethod (c$, "start0", \r
119  function () {\r
120 });\r
121 Clazz.defineMethod (c$, "run", \r
122 function () {\r
123 if (this.target != null) {\r
124 this.target.run ();\r
125 }});\r
126 Clazz.defineMethod (c$, "stop", \r
127 function () {\r
128 if ((this.threadStatus != 0) && !this.isAlive ()) {\r
129 return;\r
130 }this.stop1 ( new ThreadDeath ());\r
131 });\r
132 Clazz.defineMethod (c$, "stop", \r
133 function (obj) {\r
134 this.stop1 (obj);\r
135 }, "Throwable");\r
136 Clazz.defineMethod (c$, "stop1", \r
137  function (th) {\r
138 if (this.threadStatus != 0) {\r
139 this.resume ();\r
140 this.stop0 (th);\r
141 } else {\r
142 if (th == null) {\r
143 throw  new NullPointerException ();\r
144 }this.stopBeforeStart = true;\r
145 this.throwableFromStop = th;\r
146 }}, "Throwable");\r
147 Clazz.defineMethod (c$, "interrupt", \r
148 function () {\r
149 this.interrupt0 ();\r
150 });\r
151 c$.interrupted = Clazz.defineMethod (c$, "interrupted", \r
152 function () {\r
153 return java.lang.Thread.currentThread ().isInterruptedB (true);\r
154 });\r
155 Clazz.defineMethod (c$, "isInterrupted", \r
156 function () {\r
157 return this.isInterruptedB (false);\r
158 });\r
159 Clazz.defineMethod (c$, "isInterruptedB", \r
160  function (clearInterrupted) {\r
161 var wasInt = this.$interrupted;\r
162 if (clearInterrupted) this.$interrupted = false;\r
163 return wasInt;\r
164 }, "~B");\r
165 Clazz.defineMethod (c$, "destroy", \r
166 function () {\r
167 throw  new NoSuchMethodError ();\r
168 });\r
169 Clazz.defineMethod (c$, "isAlive", \r
170 function () {\r
171 return true;\r
172 });\r
173 Clazz.defineMethod (c$, "suspend", \r
174 function () {\r
175 this.checkAccess ();\r
176 this.suspend0 ();\r
177 });\r
178 Clazz.defineMethod (c$, "resume", \r
179 function () {\r
180 this.checkAccess ();\r
181 this.resume0 ();\r
182 });\r
183 Clazz.defineMethod (c$, "setPriority", \r
184 function (newPriority) {\r
185 var g;\r
186 this.checkAccess ();\r
187 if (newPriority > 10 || newPriority < 1) {\r
188 throw  new IllegalArgumentException ();\r
189 }if ((g = this.getThreadGroup ()) != null) {\r
190 if (newPriority > g.getMaxPriority ()) {\r
191 newPriority = g.getMaxPriority ();\r
192 }this.setPriority0 (this.priority = newPriority);\r
193 }}, "~N");\r
194 Clazz.defineMethod (c$, "getPriority", \r
195 function () {\r
196 return this.priority;\r
197 });\r
198 Clazz.defineMethod (c$, "setName", \r
199 function (name) {\r
200 this.checkAccess ();\r
201 this.name = name;\r
202 }, "~S");\r
203 Clazz.defineMethod (c$, "getName", \r
204 function () {\r
205 return this.name;\r
206 });\r
207 Clazz.defineMethod (c$, "getThreadGroup", \r
208 function () {\r
209 return this.group;\r
210 });\r
211 c$.activeCount = Clazz.defineMethod (c$, "activeCount", \r
212 function () {\r
213 return java.lang.Thread.currentThread ().getThreadGroup ().activeCount ();\r
214 });\r
215 c$.enumerate = Clazz.defineMethod (c$, "enumerate", \r
216 function (tarray) {\r
217 return java.lang.Thread.currentThread ().getThreadGroup ().enumerate (tarray);\r
218 }, "~A");\r
219 Clazz.defineMethod (c$, "countStackFrames", \r
220 function () {\r
221 return 0;\r
222 });\r
223 Clazz.defineMethod (c$, "join", \r
224 function (millis) {\r
225 var base = System.currentTimeMillis ();\r
226 var now = 0;\r
227 if (millis < 0) {\r
228 throw  new IllegalArgumentException ("timeout value is negative");\r
229 }swingjs.JSToolkit.warn ("Cannot wait in Thread");\r
230 if (millis == 0) {\r
231 while (this.isAlive ()) {\r
232 this.wait (0);\r
233 }\r
234 } else {\r
235 while (this.isAlive ()) {\r
236 var delay = millis - now;\r
237 if (delay <= 0) {\r
238 break;\r
239 }this.wait (delay);\r
240 now = System.currentTimeMillis () - base;\r
241 }\r
242 }}, "~N");\r
243 Clazz.defineMethod (c$, "join", \r
244 function (millis, nanos) {\r
245 if (millis < 0) {\r
246 throw  new IllegalArgumentException ("timeout value is negative");\r
247 }if (nanos < 0 || nanos > 999999) {\r
248 throw  new IllegalArgumentException ("nanosecond timeout value out of range");\r
249 }if (nanos >= 500000 || (nanos != 0 && millis == 0)) {\r
250 millis++;\r
251 }this.join (millis);\r
252 }, "~N,~N");\r
253 Clazz.defineMethod (c$, "join", \r
254 function () {\r
255 this.join (0);\r
256 });\r
257 c$.dumpStack = Clazz.defineMethod (c$, "dumpStack", \r
258 function () {\r
259  new Exception ("Stack trace").printStackTrace ();\r
260 });\r
261 Clazz.defineMethod (c$, "setDaemon", \r
262 function (on) {\r
263 this.checkAccess ();\r
264 if (this.isAlive ()) {\r
265 throw  new IllegalThreadStateException ();\r
266 }this.daemon = on;\r
267 }, "~B");\r
268 Clazz.defineMethod (c$, "isDaemon", \r
269 function () {\r
270 return this.daemon;\r
271 });\r
272 Clazz.defineMethod (c$, "checkAccess", \r
273 function () {\r
274 });\r
275 Clazz.overrideMethod (c$, "toString", \r
276 function () {\r
277 var group = this.getThreadGroup ();\r
278 if (group != null) {\r
279 return "Thread[" + this.getName () + "," + this.getPriority () + "," + group.getName () + "]";\r
280 } else {\r
281 return "Thread[" + this.getName () + "," + this.getPriority () + "," + "" + "]";\r
282 }});\r
283 Clazz.defineMethod (c$, "getContextClassLoader", \r
284 function () {\r
285 return null;\r
286 });\r
287 Clazz.defineMethod (c$, "setContextClassLoader", \r
288 function (cl) {\r
289 }, "ClassLoader");\r
290 c$.holdsLock = Clazz.defineMethod (c$, "holdsLock", \r
291 function (obj) {\r
292 return false;\r
293 }, "~O");\r
294 Clazz.defineMethod (c$, "getStackTrace", \r
295 function () {\r
296 return ( new Exception ()).getStackTrace ();\r
297 });\r
298 c$.getAllStackTraces = Clazz.defineMethod (c$, "getAllStackTraces", \r
299 function () {\r
300 var threads = java.lang.Thread.getThreads ();\r
301 var traces = java.lang.Thread.dumpThreads (threads);\r
302 var m =  new java.util.HashMap (threads.length);\r
303 for (var i = 0; i < threads.length; i++) {\r
304 var stackTrace = traces[i];\r
305 if (stackTrace != null) {\r
306 m.put (threads[i], stackTrace);\r
307 }}\r
308 return m;\r
309 });\r
310 c$.dumpThreads = Clazz.defineMethod (c$, "dumpThreads", \r
311  function (threads) {\r
312 return null;\r
313 }, "~A");\r
314 c$.getThreads = Clazz.defineMethod (c$, "getThreads", \r
315  function () {\r
316 return null;\r
317 });\r
318 Clazz.defineMethod (c$, "getId", \r
319 function () {\r
320 return this.tid;\r
321 });\r
322 Clazz.defineMethod (c$, "getState", \r
323 function () {\r
324 switch (this.threadStatus) {\r
325 case 0:\r
326 return java.lang.Thread.State.NEW;\r
327 case 1:\r
328 return java.lang.Thread.State.RUNNABLE;\r
329 case 2:\r
330 default:\r
331 return java.lang.Thread.State.TERMINATED;\r
332 case 3:\r
333 return java.lang.Thread.State.TIMED_WAITING;\r
334 case 4:\r
335 return java.lang.Thread.State.WAITING;\r
336 }\r
337 });\r
338 c$.setDefaultUncaughtExceptionHandler = Clazz.defineMethod (c$, "setDefaultUncaughtExceptionHandler", \r
339 function (eh) {\r
340 java.lang.Thread.defaultUncaughtExceptionHandler = eh;\r
341 }, "java.lang.Thread.UncaughtExceptionHandler");\r
342 c$.getDefaultUncaughtExceptionHandler = Clazz.defineMethod (c$, "getDefaultUncaughtExceptionHandler", \r
343 function () {\r
344 return java.lang.Thread.defaultUncaughtExceptionHandler;\r
345 });\r
346 Clazz.defineMethod (c$, "getUncaughtExceptionHandler", \r
347 function () {\r
348 return this.uncaughtExceptionHandler != null ? this.uncaughtExceptionHandler : this.group;\r
349 });\r
350 Clazz.defineMethod (c$, "setUncaughtExceptionHandler", \r
351 function (eh) {\r
352 this.checkAccess ();\r
353 this.uncaughtExceptionHandler = eh;\r
354 }, "java.lang.Thread.UncaughtExceptionHandler");\r
355 Clazz.defineMethod (c$, "setPriority0", \r
356  function (newPriority) {\r
357 }, "~N");\r
358 Clazz.defineMethod (c$, "stop0", \r
359  function (o) {\r
360 this.stopped = true;\r
361 }, "~O");\r
362 Clazz.defineMethod (c$, "suspend0", \r
363  function () {\r
364 });\r
365 Clazz.defineMethod (c$, "resume0", \r
366  function () {\r
367 });\r
368 Clazz.defineMethod (c$, "interrupt0", \r
369  function () {\r
370 this.$interrupted = true;\r
371 });\r
372 Clazz.pu$h(self.c$);\r
373 c$ = Clazz.declareType (java.lang.Thread, "State", Enum);\r
374 Clazz.defineEnumConstant (c$, "NEW", 0, []);\r
375 Clazz.defineEnumConstant (c$, "RUNNABLE", 1, []);\r
376 Clazz.defineEnumConstant (c$, "BLOCKED", 2, []);\r
377 Clazz.defineEnumConstant (c$, "WAITING", 3, []);\r
378 Clazz.defineEnumConstant (c$, "TIMED_WAITING", 4, []);\r
379 Clazz.defineEnumConstant (c$, "TERMINATED", 5, []);\r
380 c$ = Clazz.p0p ();\r
381 Clazz.declareInterface (java.lang.Thread, "UncaughtExceptionHandler");\r
382 Clazz.defineStatics (c$,\r
383 "threadInitNumber", 0,\r
384 "threadSeqNumber", 0,\r
385 "thisThread", null,\r
386 "MIN_PRIORITY", 1,\r
387 "NORM_PRIORITY", 5,\r
388 "MAX_PRIORITY", 10,\r
389 "defaultUncaughtExceptionHandler", null);\r
390 });\r