有一段老程序:
[code=java][/code]
class PrintServer implements Runnable {
private final PrintQueue requests = new PrintQueue();
public PrintServer() {
new Thread(this).start();
}
public void print(PrintJob job) {
requests.add(job);
}
public void run() {
for(;;) {
realPrint(requests.remove());
}
}
private void realPrint(PrintJob job) {
//do the real work of printing
}
}
现在需要修改这个程序,使只有在构造器中创建的线程才能成功地执行run方法,建议使用线程标识来实现。
------解决方案--------------------
PrintServer 里设置个标记flag,主方法new PrintServer ();操作之后,在将new的对象的flag设置为true,run方法里判断flag不为true就return。