我有2
关于Thread
的问题,我只是想澄清一些事情。随着下面的代码:销毁/停止线程
public class MyThread implements Runnable {
Boolean StopThread = false;
Boolean DontLoop = false;
public MyThread(){}
public void Stop(Boolean stopThread){
this.StopThread = stopThread;
}
public void ThreadDontLoop(Boolean dontLoop){
this.DontLoop = dontLoop;
}
public void run(){
if(dontLoop){
while(true){
if(StopThread){
break; //Terminate WhileLoop, This will Stop and destroy the Thread also
}
}
}else{
//Does this mean the Thread will be destroy/terminate after this condition?
}
}
}
为了开始:
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread);
thread.start();
为了启动线程,但不循环
ThreadDontLoop(false);
thread.start();
为了停止线程
myThread.Stop(true);
现在,据此LINK,这就是要停止的线程。
所以我的第一个问题是,在上面给出的代码中,如果我调用ThreadDontLoop(false);
,然后thread.start();
,这是否意味着线程将启动,但在条件之后,线程将被停止并销毁?
第二个问题是,比方说,我打电话thread.start();
,然后我呼吁myThread.Stop(true);
停止WhileLoop和销毁线程。 我没有关注如何链接停止线程,因为我将有不同的条件,但我相信我想停止线程的逻辑是正确的?
除非您需要'StopThread'或'DontLoop'为'null',否则请使用boolean而不是'Boolean'。 –
@AndyTurner - 谢谢!我不知道,现在我正在寻找“布尔”和“布尔”之间的区别。谢谢! – lopi
@lopi boolean是一个基元,只能是真或假。布尔是一个对象的引用,默认情况下它是空的。 –