在指定的毫秒數內讓當前正在執行的執行緒休眠(暫停執行),
此操作受到系統計時器和排程程式精度和準確性的影響。
sleep計時等待:
public class threadstatus catch (interruptedexception e)
system.out.println(thread.currentthread().getname() + " 時間到了自己醒了 ...");}}
}, "計時等待執行緒").start();}}
wait
方法計時等待 :
public class waittest2 catch (interruptedexception e)
system.out.println(thread.currentthread().getname() + " 1秒後, 自己醒了 ...");}}
}}, "計時等待").start();}}
乙個正在阻塞等待乙個監視器鎖(鎖物件)的執行緒處於這一狀態。
演示:
public class waittest3 catch (interruptedexception e)
// 喚醒
system.out.println(thread.currentthread().getname() + " 從死等狀態被喚醒 ...");}}
}}, "無限等待").start();
// 模擬喚醒執行緒
new thread(new runnable() catch (interruptedexception e)
// 鎖環境
synchronized (lock) }}
}, "喚醒執行緒").start();}}
javaSE 多執行緒(守護執行緒)
我們一般使用多執行緒,都是while 的死迴圈,想要結束執行緒,只需退出死迴圈即可 當執行緒中呼叫了sleep 方法或者 wait 方法,當前的執行緒就會進入凍結狀態,這個執行緒就結束不了 呼叫thread 物件的interrupt 方法,可以強制解凍,此時 run 方法中需要捕獲到 interru...
javaSE 多執行緒之執行緒池
1 使用語法 publicstaticvoiduseexecutorservice executorservice shutdown 輸出 pool 1 thread 3 pool 1 thread 2 pool 1 thread 1 pool 1 thread 3 pool 1 thread 2 ...
JavaSE 20190511 多執行緒
一.多執行緒 1.多執行緒 多工同時執行就是多執行緒,如果沒有任務,就不需要使用多執行緒 2.執行緒和程序之間的區別 執行緒 cpu排程的最小單位 程序 資源分配的最小單位 乙個程序可以包含1 n個執行緒 3.執行緒開啟的方式 1 繼承thread類,重寫run 方法 建立子類物件,呼叫start ...