📄 rethread.java
字号:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.util.*;
public class ReThread implements Runnable {
protected Runnable target;
public ReThread (Runnable target) {
this.target = target;
}
private static int id = 0;
private static synchronized int getID () { return id ++; }
private static Vector threads = new Vector ();
private ReThread reThread;
private Thread thread;
public synchronized void start () {
if ((thread == null) && (reThread == null)) {
synchronized (threads) {
if (threads.isEmpty ()) {
thread = new Thread (this, "ReThread-" + getID ());
thread.start ();
} else {
reThread = (ReThread) threads.lastElement ();
threads.setSize (threads.size () - 1);
reThread.start0 (this);
}
}
}
}
protected synchronized void start0 (ReThread reThread) {
this.reThread = reThread;
target = reThread.target;
notify ();
}
public synchronized void interrupt () {
if ((target != null) && ((thread != null) ^ (reThread != null))) {
if (thread != null) {
thread.interrupt ();
} else {
reThread.interrupt0 (this);
}
}
}
protected synchronized void interrupt0 (ReThread reThread) {
if ((target != null) && (reThread == this.reThread)) {
thread.interrupt ();
}
}
public void run () {
while (true) {
try {
target.run ();
} catch (RuntimeException ex) {
ex.printStackTrace ();
}
waitForTarget ();
}
}
protected synchronized void waitForTarget () {
target = null;
threads.addElement (this);
while (target == null) {
try {
wait ();
} catch (InterruptedException ignored) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -