corda/classpath/java/lang/Thread.java

98 lines
2.1 KiB
Java
Raw Normal View History

package java.lang;
2007-07-21 20:44:39 +00:00
import java.util.Map;
import java.util.WeakHashMap;
public class Thread implements Runnable {
private long peer;
private final Runnable task;
2007-07-21 20:44:39 +00:00
private Map<ThreadLocal, Object> locals;
private boolean interrupted;
2007-07-28 21:28:25 +00:00
private Object sleepLock;
private ClassLoader classLoader;
public Thread(Runnable task) {
this.task = task;
2007-07-28 21:28:25 +00:00
Thread current = currentThread();
Map<ThreadLocal, Object> map = current.locals;
2007-07-21 22:36:51 +00:00
if (map != null) {
for (Map.Entry<ThreadLocal, Object> e: map.entrySet()) {
if (e.getKey() instanceof InheritableThreadLocal) {
2007-07-28 16:10:13 +00:00
InheritableThreadLocal itl = (InheritableThreadLocal) e.getKey();
locals().put(itl, itl.childValue(e.getValue()));
2007-07-21 22:36:51 +00:00
}
}
}
classLoader = current.classLoader;
}
2007-07-21 22:36:51 +00:00
public synchronized void start() {
if (peer != 0) {
throw new IllegalStateException("thread already started");
}
peer = doStart();
if (peer == 0) {
throw new RuntimeException("unable to start native thread");
}
2007-07-21 22:36:51 +00:00
}
private native long doStart();
public void run() {
if (task != null) {
task.run();
}
}
public ClassLoader getContextClassLoader() {
return classLoader;
}
public void setContextClassLoader(ClassLoader v) {
classLoader = v;
}
2007-07-21 20:44:39 +00:00
public Map<ThreadLocal, Object> locals() {
if (locals == null) {
locals = new WeakHashMap();
}
return locals;
}
public static native Thread currentThread();
2007-07-28 21:28:25 +00:00
private static native void interrupt(long peer);
public synchronized void interrupt() {
if (peer != 0) {
interrupt(peer);
} else {
interrupted = true;
}
}
public static boolean interrupted() {
Thread t = currentThread();
synchronized (t) {
boolean v = t.interrupted;
t.interrupted = false;
return v;
}
}
2007-07-28 16:10:13 +00:00
public static void sleep(long milliseconds) throws InterruptedException {
Thread t = currentThread();
if (t.sleepLock == null) {
t.sleepLock = new Object();
}
synchronized (t.sleepLock) {
t.sleepLock.wait(milliseconds);
}
}
}