java 正常的线程停止操作思想

publicclass ttt{

<wbr><wbr> runnnr<wbr> = new runn();</wbr></wbr></wbr>

<wbr><wbr> threah t =new threah(r);</wbr></wbr>

<wbr><wbr><wbr>t.start;</wbr></wbr></wbr>

<wbr><wbr><wbr><wbr>.......................</wbr></wbr></wbr></wbr>

}

<wbr></wbr>

classrunnn implements Runnable{

<wbr> private booleanflag<wbr>= ture;</wbr></wbr>

<wbr> public voidrun(){</wbr>

<wbr><wbr><wbr>....</wbr></wbr></wbr>

<wbr><wbr><wbr>while(flag == true){</wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr>.........</wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr>}</wbr></wbr></wbr>

<wbr> }</wbr>

<wbr><wbr> public voidshutdown(){</wbr></wbr>

<wbr><wbr><wbr><wbr>flag = flase;<wbr></wbr></wbr></wbr></wbr></wbr>

<wbr> }</wbr>

}

<wbr></wbr>

<wbr></wbr>

利用条件来使RUN方法完结是终止一个线程的最王道的方法,比如在SOCKET编程中,可以判断SOCKET管道是否还连接等等

<wbr></wbr>

<wbr></wbr>

当然,也可以联合用IF,来增加结束线程的条件。

<wbr></wbr>

<wbr><wbr><wbr>while(flag == true){</wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr>.........</wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr>if(STOP){//stop为BOOLEAN型,默为FALSE,搞个方法让它能成TRUE,方法就BREAK了。</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>break;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr>}</wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>public void setStop(boolean stop){</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>this.stop = stop;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

}

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>privateboolean stop = false;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(java)