Thread类中有 isAlive()方法 public final boolean isAlive() Tests if this thread is alive. A thread is alive if it has been started and has not yet died. Returns: true if this thread is alive; false otherwise.
用isAlive方法判断线程是否终结
严重支持laughsmile(海边的星空) ( )
一个笨点的方法是设置一个状态参数 以下代码仅供参考,少了很多东西. public int status = NOT_RUN; public void run(){ status =RUNNING; ...... status =RUN_OVER; } public String getStr(){ while(status != RUN_OVER){ Thread.sleep(50); } }
thread t =new thread()
t.start()
t.gets()//这个时候返回null
我希望run执行完毕
public synchronized String run(){}
public synchronized String get(){}
利用生产者与消费者同步解决。
public synchronized String run(){}
public synchronized String get(){}
我觉得java语言已经提供现成得东西了,利用join就行了。
public final boolean isAlive()
Tests if this thread is alive. A thread is alive if it has been started and has not yet died. Returns:
true if this thread is alive; false otherwise.
以下代码仅供参考,少了很多东西.
public int status = NOT_RUN;
public void run(){
status =RUNNING;
......
status =RUN_OVER;
}
public String getStr(){
while(status != RUN_OVER){
Thread.sleep(50);
}
}