多线程编程――实战篇(一) (六)

2014-11-24 11:49:53 · 作者: · 浏览: 46
线程同步的时候我们有一个叫“二次惰性检测”(double check),能在提高效率的基础上又确保线程真正中同步控制中。那么我把线程正确退出的方法称为“双重安全退出”,即不以isInterrupted()为循环条件。而以一个标记作为循环条件:

class MyThread extend Thread{

private boolean isInterrupted = false;//这一句以后要修改

public void interrupt(){

isInterrupted = true;

super.interrupt();

}

public void run(){

while(!isInterrupted){

try{

正常工作

}catch(Exception e){

//nothing

}

finally{

}

}

}

}

  试试这段程序,可以正确工作吗

  对于这段程序仍然还有很多可说的地方,先到这里吧。