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