執行緒狀態轉換

2022-09-08 00:51:29 字數 2684 閱讀 1042

@test

public void testthreadstate1() throws exception catch (interruptedexception e)

system.out.println("1" + "-" + thread.currentthread().getname() + "的狀態是:" + thread.currentthread().getstate());

}},"thread4teststate");

system.out.println("2" + "-" + thread4teststate.getname() + "的狀態是:" + thread.getstate());

thread.start();

system.out.println("3" + "-" + thread4teststate.getname() + "的狀態是:" + thread.getstate());

thread.sleep(2000);

system.out.println("4" + "-" + thread4teststate.getname() + "的狀態是:" + thread.getstate());

system.in.read();

}

上面的輸出是:

@test

public void testthreadstate2() throws exception catch (interruptedexception e)

}system.out.println("2" + "-" + thread.currentthread().getname() + "釋放了鎖,當前的狀態是:" + thread.currentthread().getstate());

}}, "thread4teststate1");

thread thread4teststate2 = new thread(new runnable() catch (interruptedexception e)

}system.out.println("4" + "-" + thread.currentthread().getname() + "釋放了鎖,當前的狀態是:" + thread.currentthread().getstate());

}}, "thread4teststate2");

thread4teststate1.start();

thread4teststate2.start();

thread.sleep(500);

system.out.println("5" + "-" + thread4teststate1.getname() + "當前的狀態是:" + thread4teststate1.getstate());

system.out.println("6" + "-" + thread4teststate2.getname() + "當前的狀態是:" + thread4teststate2.getstate());

system.in.read();

}

上面的輸出是:

@test

public void testthreadstate3() throws exception catch (interruptedexception e)

system.out.println("2" + "-" + thread.currentthread().getname() + "即將釋放鎖,當前的狀態是:" + thread.currentthread().getstate());}}

}, "thread4teststate1");

thread4teststate1.start();

thread.sleep(300);

system.out.println("3" + "-" + thread4teststate1.getname() + "當前的狀態是:" + thread4teststate1.getstate());

system.in.read();

}

上面的輸出是:

@test

public void testthreadstate3() throws exception catch (interruptedexception e)

system.out.println("2" + "-" + thread.currentthread().getname() + "即將釋放鎖,當前的狀態是:" + thread.currentthread().getstate());}}

}, "thread4teststate1");

thread4teststate1.start();

thread.sleep(50);

system.out.println("3" + "-" + thread4teststate1.getname() + "當前的狀態是:" + thread4teststate1.getstate());

synchronized (lock)

system.out.println("4" + "-" + thread4teststate1.getname() + "當前的狀態是:" + thread4teststate1.getstate());

system.in.read();

}

上面的輸出是:

執行緒狀態轉換

退出方法 沒有設定 timeout 引數的 object.wait 方法 object.notify object.notifyall 沒有設定 timeout 引數的 thread.join 方法 被呼叫的執行緒執行完畢 locksupport.park 方法 locksupport.unpark...

執行緒狀態轉換

建立後尚未啟動。可能正在執行,也可能正在等待 cpu 時間片。包含了作業系統執行緒狀態中的 running 和 ready。等待獲取乙個排它鎖,如果其執行緒釋放了鎖就會結束此狀態。等待其它執行緒顯式地喚醒,否則不會被分配 cpu 時間片。進入方法 退出方法 沒有設定 timeout 引數的 obje...

執行緒狀態轉換

runnable waiting 執行緒用synchronized obj 獲取了物件鎖後 呼叫obj.wait 方法時,t 執行緒從runnable waiting 呼叫obj.notify obj.notifyall t.interrupt 時 競爭鎖成功,t 執行緒從waiting runna...