b6e3356a65372325bcc620852b1e83382f77befa
[jalviewjs.git] / site / swingjs / j2s / java / lang / ThreadGroup.js
1 Clazz.declarePackage ("java.lang");\r
2 Clazz.load (["java.lang.Thread"], "java.lang.ThreadGroup", ["java.lang.IllegalThreadStateException", "$.ThreadDeath", "java.util.Arrays"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.parent = null;\r
5 this.name = null;\r
6 this.maxPriority = 10;\r
7 this.destroyed = false;\r
8 this.daemon = false;\r
9 this.vmAllowSuspension = false;\r
10 this.nUnstartedThreads = 0;\r
11 this.nthreads = 0;\r
12 this.threads = null;\r
13 this.ngroups = 0;\r
14 this.groups = null;\r
15 this.html5Applet = null;\r
16 Clazz.instantialize (this, arguments);\r
17 }, java.lang, "ThreadGroup", null, java.lang.Thread.UncaughtExceptionHandler);\r
18 Clazz.makeConstructor (c$, \r
19 function (name) {\r
20 this.construct (java.lang.Thread.currentThread ().getThreadGroup (), name);\r
21 }, "~S");\r
22 Clazz.makeConstructor (c$, \r
23 function (parent, name) {\r
24 this.name = name;\r
25 this.parent = parent;\r
26 if (parent != null) {\r
27 this.maxPriority = parent.maxPriority;\r
28 this.daemon = parent.daemon;\r
29 this.vmAllowSuspension = parent.vmAllowSuspension;\r
30 parent.add (this);\r
31 }}, "java.lang.ThreadGroup,~S");\r
32 Clazz.defineMethod (c$, "getName", \r
33 function () {\r
34 return this.name;\r
35 });\r
36 Clazz.defineMethod (c$, "getParent", \r
37 function () {\r
38 if (this.parent != null) this.parent.checkAccess ();\r
39 return this.parent;\r
40 });\r
41 Clazz.defineMethod (c$, "getMaxPriority", \r
42 function () {\r
43 return this.maxPriority;\r
44 });\r
45 Clazz.defineMethod (c$, "isDaemon", \r
46 function () {\r
47 return this.daemon;\r
48 });\r
49 Clazz.defineMethod (c$, "isDestroyed", \r
50 function () {\r
51 return this.destroyed;\r
52 });\r
53 Clazz.defineMethod (c$, "setDaemon", \r
54 function (daemon) {\r
55 this.checkAccess ();\r
56 this.daemon = daemon;\r
57 }, "~B");\r
58 Clazz.defineMethod (c$, "setMaxPriority", \r
59 function (pri) {\r
60 var ngroupsSnapshot;\r
61 var groupsSnapshot;\r
62 {\r
63 this.checkAccess ();\r
64 if (pri < 1 || pri > 10) {\r
65 return;\r
66 }this.maxPriority = (this.parent != null) ? Math.min (pri, this.parent.maxPriority) : pri;\r
67 ngroupsSnapshot = this.ngroups;\r
68 if (this.groups != null) {\r
69 groupsSnapshot = java.util.Arrays.copyOf (this.groups, ngroupsSnapshot);\r
70 } else {\r
71 groupsSnapshot = null;\r
72 }}for (var i = 0; i < ngroupsSnapshot; i++) {\r
73 groupsSnapshot[i].setMaxPriority (pri);\r
74 }\r
75 }, "~N");\r
76 Clazz.defineMethod (c$, "parentOf", \r
77 function (g) {\r
78 for (; g != null; g = g.parent) {\r
79 if (g === this) {\r
80 return true;\r
81 }}\r
82 return false;\r
83 }, "java.lang.ThreadGroup");\r
84 Clazz.defineMethod (c$, "checkAccess", \r
85 function () {\r
86 });\r
87 Clazz.defineMethod (c$, "activeCount", \r
88 function () {\r
89 var result;\r
90 var ngroupsSnapshot;\r
91 var groupsSnapshot;\r
92 {\r
93 if (this.destroyed) {\r
94 return 0;\r
95 }result = this.nthreads;\r
96 ngroupsSnapshot = this.ngroups;\r
97 if (this.groups != null) {\r
98 groupsSnapshot = java.util.Arrays.copyOf (this.groups, ngroupsSnapshot);\r
99 } else {\r
100 groupsSnapshot = null;\r
101 }}for (var i = 0; i < ngroupsSnapshot; i++) {\r
102 result += groupsSnapshot[i].activeCount ();\r
103 }\r
104 return result;\r
105 });\r
106 Clazz.defineMethod (c$, "enumerate", \r
107 function (list) {\r
108 this.checkAccess ();\r
109 return this.enumerate (list, 0, true);\r
110 }, "~A");\r
111 Clazz.defineMethod (c$, "enumerate", \r
112 function (list, recurse) {\r
113 this.checkAccess ();\r
114 return this.enumerate (list, 0, recurse);\r
115 }, "~A,~B");\r
116 Clazz.defineMethod (c$, "enumerate", \r
117  function (list, n, recurse) {\r
118 var ngroupsSnapshot = 0;\r
119 var groupsSnapshot = null;\r
120 {\r
121 if (this.destroyed) {\r
122 return 0;\r
123 }var nt = this.nthreads;\r
124 if (nt > list.length - n) {\r
125 nt = list.length - n;\r
126 }for (var i = 0; i < nt; i++) {\r
127 if (this.threads[i].isAlive ()) {\r
128 list[n++] = this.threads[i];\r
129 }}\r
130 if (recurse) {\r
131 ngroupsSnapshot = this.ngroups;\r
132 if (this.groups != null) {\r
133 groupsSnapshot = java.util.Arrays.copyOf (this.groups, ngroupsSnapshot);\r
134 } else {\r
135 groupsSnapshot = null;\r
136 }}}if (recurse) {\r
137 for (var i = 0; i < ngroupsSnapshot; i++) {\r
138 n = groupsSnapshot[i].enumerate (list, n, true);\r
139 }\r
140 }return n;\r
141 }, "~A,~N,~B");\r
142 Clazz.defineMethod (c$, "activeGroupCount", \r
143 function () {\r
144 var ngroupsSnapshot;\r
145 var groupsSnapshot;\r
146 {\r
147 if (this.destroyed) {\r
148 return 0;\r
149 }ngroupsSnapshot = this.ngroups;\r
150 if (this.groups != null) {\r
151 groupsSnapshot = java.util.Arrays.copyOf (this.groups, ngroupsSnapshot);\r
152 } else {\r
153 groupsSnapshot = null;\r
154 }}var n = ngroupsSnapshot;\r
155 for (var i = 0; i < ngroupsSnapshot; i++) {\r
156 n += groupsSnapshot[i].activeGroupCount ();\r
157 }\r
158 return n;\r
159 });\r
160 Clazz.defineMethod (c$, "enumerate", \r
161 function (list) {\r
162 this.checkAccess ();\r
163 return this.enumerate (list, 0, true);\r
164 }, "~A");\r
165 Clazz.defineMethod (c$, "enumerate", \r
166 function (list, recurse) {\r
167 this.checkAccess ();\r
168 return this.enumerate (list, 0, recurse);\r
169 }, "~A,~B");\r
170 Clazz.defineMethod (c$, "enumerate", \r
171  function (list, n, recurse) {\r
172 var ngroupsSnapshot = 0;\r
173 var groupsSnapshot = null;\r
174 {\r
175 if (this.destroyed) {\r
176 return 0;\r
177 }var ng = this.ngroups;\r
178 if (ng > list.length - n) {\r
179 ng = list.length - n;\r
180 }if (ng > 0) {\r
181 System.arraycopy (this.groups, 0, list, n, ng);\r
182 n += ng;\r
183 }if (recurse) {\r
184 ngroupsSnapshot = this.ngroups;\r
185 if (this.groups != null) {\r
186 groupsSnapshot = java.util.Arrays.copyOf (this.groups, ngroupsSnapshot);\r
187 } else {\r
188 groupsSnapshot = null;\r
189 }}}if (recurse) {\r
190 for (var i = 0; i < ngroupsSnapshot; i++) {\r
191 n = groupsSnapshot[i].enumerate (list, n, true);\r
192 }\r
193 }return n;\r
194 }, "~A,~N,~B");\r
195 Clazz.defineMethod (c$, "stop", \r
196 function () {\r
197 if (this.stopOrSuspend (false)) java.lang.Thread.currentThread ().stop ();\r
198 });\r
199 Clazz.defineMethod (c$, "interrupt", \r
200 function () {\r
201 var ngroupsSnapshot;\r
202 var groupsSnapshot;\r
203 {\r
204 this.checkAccess ();\r
205 for (var i = 0; i < this.nthreads; i++) {\r
206 this.threads[i].interrupt ();\r
207 }\r
208 ngroupsSnapshot = this.ngroups;\r
209 if (this.groups != null) {\r
210 groupsSnapshot = java.util.Arrays.copyOf (this.groups, ngroupsSnapshot);\r
211 } else {\r
212 groupsSnapshot = null;\r
213 }}for (var i = 0; i < ngroupsSnapshot; i++) {\r
214 groupsSnapshot[i].interrupt ();\r
215 }\r
216 });\r
217 Clazz.defineMethod (c$, "suspend", \r
218 function () {\r
219 if (this.stopOrSuspend (true)) java.lang.Thread.currentThread ().suspend ();\r
220 });\r
221 Clazz.defineMethod (c$, "stopOrSuspend", \r
222  function (suspend) {\r
223 var suicide = false;\r
224 var us = java.lang.Thread.currentThread ();\r
225 var ngroupsSnapshot;\r
226 var groupsSnapshot = null;\r
227 {\r
228 this.checkAccess ();\r
229 for (var i = 0; i < this.nthreads; i++) {\r
230 if (this.threads[i] === us) suicide = true;\r
231  else if (suspend) this.threads[i].suspend ();\r
232  else this.threads[i].stop ();\r
233 }\r
234 ngroupsSnapshot = this.ngroups;\r
235 if (this.groups != null) {\r
236 groupsSnapshot = java.util.Arrays.copyOf (this.groups, ngroupsSnapshot);\r
237 }}for (var i = 0; i < ngroupsSnapshot; i++) suicide = groupsSnapshot[i].stopOrSuspend (suspend) || suicide;\r
238 \r
239 return suicide;\r
240 }, "~B");\r
241 Clazz.defineMethod (c$, "resume", \r
242 function () {\r
243 var ngroupsSnapshot;\r
244 var groupsSnapshot;\r
245 {\r
246 this.checkAccess ();\r
247 for (var i = 0; i < this.nthreads; i++) {\r
248 this.threads[i].resume ();\r
249 }\r
250 ngroupsSnapshot = this.ngroups;\r
251 if (this.groups != null) {\r
252 groupsSnapshot = java.util.Arrays.copyOf (this.groups, ngroupsSnapshot);\r
253 } else {\r
254 groupsSnapshot = null;\r
255 }}for (var i = 0; i < ngroupsSnapshot; i++) {\r
256 groupsSnapshot[i].resume ();\r
257 }\r
258 });\r
259 Clazz.defineMethod (c$, "destroy", \r
260 function () {\r
261 var ngroupsSnapshot;\r
262 var groupsSnapshot;\r
263 {\r
264 this.checkAccess ();\r
265 if (this.destroyed || (this.nthreads > 0)) {\r
266 throw  new IllegalThreadStateException ();\r
267 }ngroupsSnapshot = this.ngroups;\r
268 if (this.groups != null) {\r
269 groupsSnapshot = java.util.Arrays.copyOf (this.groups, ngroupsSnapshot);\r
270 } else {\r
271 groupsSnapshot = null;\r
272 }if (this.parent != null) {\r
273 this.destroyed = true;\r
274 this.ngroups = 0;\r
275 this.groups = null;\r
276 this.nthreads = 0;\r
277 this.threads = null;\r
278 }}for (var i = 0; i < ngroupsSnapshot; i += 1) {\r
279 groupsSnapshot[i].destroy ();\r
280 }\r
281 if (this.parent != null) {\r
282 this.parent.remove (this);\r
283 }});\r
284 Clazz.defineMethod (c$, "add", \r
285  function (g) {\r
286 {\r
287 if (this.destroyed) {\r
288 throw  new IllegalThreadStateException ();\r
289 }if (this.groups == null) {\r
290 this.groups =  new Array (4);\r
291 } else if (this.ngroups == this.groups.length) {\r
292 this.groups = java.util.Arrays.copyOf (this.groups, this.ngroups * 2);\r
293 }this.groups[this.ngroups] = g;\r
294 this.ngroups++;\r
295 }}, "java.lang.ThreadGroup");\r
296 Clazz.defineMethod (c$, "remove", \r
297  function (g) {\r
298 {\r
299 if (this.destroyed) {\r
300 return;\r
301 }for (var i = 0; i < this.ngroups; i++) {\r
302 if (this.groups[i] === g) {\r
303 this.ngroups -= 1;\r
304 System.arraycopy (this.groups, i + 1, this.groups, i, this.ngroups - i);\r
305 this.groups[this.ngroups] = null;\r
306 break;\r
307 }}\r
308 if (this.nthreads == 0) {\r
309 this.notifyAll ();\r
310 }if (this.daemon && (this.nthreads == 0) && (this.nUnstartedThreads == 0) && (this.ngroups == 0)) {\r
311 this.destroy ();\r
312 }}}, "java.lang.ThreadGroup");\r
313 Clazz.defineMethod (c$, "addUnstarted", \r
314 function () {\r
315 {\r
316 if (this.destroyed) {\r
317 throw  new IllegalThreadStateException ();\r
318 }this.nUnstartedThreads++;\r
319 }});\r
320 Clazz.defineMethod (c$, "add", \r
321 function (t) {\r
322 {\r
323 if (this.destroyed) {\r
324 throw  new IllegalThreadStateException ();\r
325 }if (this.threads == null) {\r
326 this.threads =  new Array (4);\r
327 } else if (this.nthreads == this.threads.length) {\r
328 this.threads = java.util.Arrays.copyOf (this.threads, this.nthreads * 2);\r
329 }this.threads[this.nthreads] = t;\r
330 this.nthreads++;\r
331 this.nUnstartedThreads--;\r
332 }}, "java.lang.Thread");\r
333 Clazz.defineMethod (c$, "remove", \r
334 function (t) {\r
335 {\r
336 if (this.destroyed) {\r
337 return;\r
338 }for (var i = 0; i < this.nthreads; i++) {\r
339 if (this.threads[i] === t) {\r
340 System.arraycopy (this.threads, i + 1, this.threads, i, --this.nthreads - i);\r
341 this.threads[this.nthreads] = null;\r
342 break;\r
343 }}\r
344 if (this.nthreads == 0) {\r
345 this.notifyAll ();\r
346 }if (this.daemon && (this.nthreads == 0) && (this.nUnstartedThreads == 0) && (this.ngroups == 0)) {\r
347 this.destroy ();\r
348 }}}, "java.lang.Thread");\r
349 Clazz.defineMethod (c$, "uncaughtException", \r
350 function (t, e) {\r
351 if (this.parent != null) {\r
352 this.parent.uncaughtException (t, e);\r
353 } else {\r
354 var ueh = java.lang.Thread.getDefaultUncaughtExceptionHandler ();\r
355 if (ueh != null) {\r
356 ueh.uncaughtException (t, e);\r
357 } else if (!(Clazz.instanceOf (e, ThreadDeath))) {\r
358 System.err.print ("Exception in thread \"" + t.getName () + "\" ");\r
359 e.printStackTrace (System.err);\r
360 }}}, "java.lang.Thread,Throwable");\r
361 Clazz.overrideMethod (c$, "toString", \r
362 function () {\r
363 return this.getClass ().getName () + "[name=" + this.getName () + ",maxpri=" + this.maxPriority + ",html5Applet=" + this.html5Applet + "]";\r
364 });\r
365 });\r