我正在創建一個程序,並首次使用細節中的線程,並陷入了一種情況。請幫助我。 我有一個處於等待狀態的線程。現在在某些情況下,我想要殺死或喚醒線程並從另一個類繼續。爲此,我保存了線程對象。我不知道如何執行此操作。試圖通知線程,但得到exception.Here是我的代碼:從睡夢中醒來的線程
類別之一:
Thread t= new Thread(new Runnable() {
@Override
public void run() {
try{
Thread.sleep(VariableClass.THREAD_WAIT_SECONDS);
if(message !=null)
message_status = message.getStatus();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
//do other stuff and save the thread object
VariableClass.threads.remove(message.getUniqueId());
}
});
t.start();
VariableClass.threads.put(pojo.getUniqueId(),t);
類二:
Thread t =VariableClass.threads.get(tempId);
t.notify();
我只是想恢復或殺死線程。
我想你應該使用't.interrupt()' – SpaceTrucker
這可能是一個XY問題的實例(http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)你真正的目標是什麼?如果你想從一個線程傳遞消息到另一個線程,那麼你可能應該使用某種類型的隊列(例如java.util.concurrent.SynchronousQueue或java.util.concurrent.BlockingQueue)。 –