執行緒狀態的扭轉

2021-09-25 17:55:00 字數 2091 閱讀 4136

第一種:new-runnable-terminated

第二種:新建-執行-等待-執行-終止

第三種:新建-執行-阻塞-執行-終止

});system.out.println("#######################thread1沒有呼叫start當前執行緒狀態" + thread1.getstate().tostring());

thread1.start();

thread.sleep(3000);

system.out.println("等待2秒,再看看執行緒1狀態" + thread1.getstate().tostring());

system.out.println();

system.out.println("#######################第二種:新建-執行-等待-執行-終止");

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

system.out.println("thread2當前執行緒狀態" + thread.currentthread().getstate().tostring());

system.out.println("thread2執行了");

}});

system.out.println("thread2沒有呼叫start當前執行緒狀態" + thread2.getstate().tostring());

thread2.start();

system.out.println("呼叫start方法,thread2當前狀態" + thread2.getstate().tostring());

thread.sleep(200);

system.out.println("等待200毫秒,再看看執行緒2狀態" + thread2.getstate().tostring());

thread.sleep(3000);

system.out.println("等待3秒,再看看執行緒2狀態" + thread2.getstate().tostring());

system.out.println();

system.out.println("#######################第三種:新建-執行-阻塞-執行-終止");

thread thread3 = new thread(new runnable()

}});

synchronized (threadstatusdemo1.class)

thread.sleep(3000);

system.out.println("等待3秒,再看看執行緒2狀態" + thread3.getstate().tostring());

}#######################thread1沒有呼叫start當前執行緒狀態new

thread1當前執行緒狀態runnable

執行緒1執行了

等待2秒,再看看執行緒1狀態terminated

#######################第二種:新建-執行-等待-執行-終止

thread2沒有呼叫start當前執行緒狀態new

呼叫start方法,thread2當前狀態runnable

等待200毫秒,再看看執行緒2狀態timed_waiting

thread2當前執行緒狀態runnable

thread2執行了

等待3秒,再看看執行緒2狀態terminated

#######################第三種:新建-執行-阻塞-執行-終止

thread3沒有呼叫start當前執行緒狀態new

呼叫start方法,thread3當前狀態runnable

等待200毫秒,再看看執行緒3狀態blocked

thread3當前執行緒狀態runnable

thread3執行了

等待3秒,再看看執行緒2狀態terminated

執行緒的狀態

執行緒的狀態 概念 乙個時刻,cpu只能運算乙個執行緒的任務。對於執行緒來說,有cpu的執行資格或者cpu的執行權。cpu的執行資格 可以被cpu處理,但還沒有輪到自己,在處理佇列中排隊。cpu的執行權 正在獲取cpu的資格 執行緒有五種狀態 1 被建立。在這個時候,執行緒剛剛被建立出來,開闢了記憶...

執行緒的狀態

有時候需要一些工具幫助我們觀察執行緒和程序的狀態 spy 開啟預設是視窗 旁邊是 程序和執行緒 選擇乙個程序 右鍵屬性 常規 不重要 記憶體 虛擬位元組的記憶體 專用位元組 可用位元組 頁檔案 空格鍵 記憶體資訊 選擇乙個執行緒 右鍵屬性 執行緒id 程序id 起始位址 執行緒狀態 上下文開關 執行...

執行緒的狀態

學習自 新建new thread 就緒thread.start 執行using cpu,就算時間片在切換,他在不使用當前時間片的時候,依然被定義成執行狀態 阻塞比如釋放資源,比如object.wait。需要重新轉化為就緒狀態才有機會轉化成執行狀態 死亡阻塞的幾種情況 1.this.wait,該執行緒...