View Javadoc

1   // Thread.java, created Thu Jul  4  4:50:03 2002 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.ClassLib.Common.java.lang;
5   
6   import joeq.Runtime.Unsafe;
7   import joeq.Scheduler.jq_Thread;
8   import jwutil.util.Assert;
9   
10  /***
11   * Thread
12   *
13   * @author  John Whaley <jwhaley@alum.mit.edu>
14   * @version $Id: Thread.java 1941 2004-09-30 03:37:06Z joewhaley $
15   */
16  public abstract class Thread {
17  
18      // additional fields
19      public final jq_Thread jq_thread;
20      
21      private boolean daemon;
22      
23      private Thread(jq_Thread t) {
24          this.jq_thread = t;
25      }
26      
27      private native void checkAccess();
28      private static synchronized native int nextThreadNum();
29      private native void init(java.lang.ThreadGroup g, java.lang.Runnable target, java.lang.String name);
30      
31      // overridden constructors
32      public Thread() {
33          java.lang.Object o = this;
34          jq_Thread t = new jq_Thread((java.lang.Thread)o);
35          this.jq_thread = t;
36          int n = nextThreadNum();
37          this.init(null, null, "Thread-"+n);
38          t.init();
39      }
40      public Thread(java.lang.Runnable target) {
41          java.lang.Object o = this;
42          jq_Thread t = new jq_Thread((java.lang.Thread)o);
43          this.jq_thread = t;
44          int n = nextThreadNum();
45          this.init(null, target, "Thread-"+n);
46          t.init();
47      }
48      public Thread(java.lang.ThreadGroup group, java.lang.Runnable target) {
49          java.lang.Object o = this;
50          jq_Thread t = new jq_Thread((java.lang.Thread)o);
51          this.jq_thread = t;
52          int n = nextThreadNum();
53          this.init(group, target, "Thread-"+n);
54          t.init();
55      }
56      public Thread(java.lang.String name) {
57          java.lang.Object o = this;
58          jq_Thread t = new jq_Thread((java.lang.Thread)o);
59          this.jq_thread = t;
60          this.init(null, null, name);
61          t.init();
62      }
63      public Thread(java.lang.ThreadGroup group, java.lang.String name) {
64          java.lang.Object o = this;
65          jq_Thread t = new jq_Thread((java.lang.Thread)o);
66          this.jq_thread = t;
67          this.init(group, null, name);
68          t.init();
69      }
70      public Thread(java.lang.Runnable target, java.lang.String name) {
71          java.lang.Object o = this;
72          jq_Thread t = new jq_Thread((java.lang.Thread)o);
73          this.jq_thread = t;
74          this.init(null, target, name);
75          t.init();
76      }
77      public Thread(java.lang.ThreadGroup group, java.lang.Runnable target, java.lang.String name) {
78          java.lang.Object o = this;
79          jq_Thread t = new jq_Thread((java.lang.Thread)o);
80          this.jq_thread = t;
81          this.init(group, target, name);
82          t.init();
83      }
84      
85      // native method implementations
86      private static void registerNatives() {}
87      public static java.lang.Thread currentThread() { return Unsafe.getThreadBlock().getJavaLangThreadObject(); }
88      public static void yield() { Unsafe.getThreadBlock().yield(); }
89      public static void sleep(long millis) throws InterruptedException { Unsafe.getThreadBlock().sleep(millis); }
90      public synchronized void start() {
91          jq_Thread jq_thread = this.jq_thread;
92          jq_thread.start();
93      }
94      private boolean isInterrupted(boolean ClearInterrupted) {
95          jq_Thread jq_thread = this.jq_thread;
96          return jq_thread.isInterrupted(ClearInterrupted);
97      }
98      public final boolean isAlive() {
99          jq_Thread jq_thread = this.jq_thread;
100         return jq_thread.isAlive();
101     }
102     public final void setDaemon(boolean b) {
103         this.checkAccess();
104         if (this.isAlive()) {
105             throw new java.lang.IllegalThreadStateException();
106         }
107         this.daemon = b;
108         jq_Thread jq_thread = this.jq_thread;
109         jq_thread.setDaemon(b);
110     }
111     public int countStackFrames() {
112         jq_Thread jq_thread = this.jq_thread;
113         return jq_thread.countStackFrames();
114     }
115     private void setPriority0(int newPriority) {
116         jq_Thread jq_thread = this.jq_thread;
117         Assert._assert(newPriority >= 1);
118         Assert._assert(newPriority <= 10);
119         jq_thread.setPriority(newPriority - 1);
120     }
121     private void stop0(java.lang.Object o) {
122         jq_Thread jq_thread = this.jq_thread;
123         jq_thread.stop(o);
124     }
125     private void suspend0() {
126         jq_Thread jq_thread = this.jq_thread;
127         jq_thread.suspend();
128     }
129     private void resume0() {
130         jq_Thread jq_thread = this.jq_thread;
131         jq_thread.resume();
132     }
133     private void interrupt0() {
134         jq_Thread jq_thread = this.jq_thread;
135         jq_thread.interrupt();
136     }
137 }