[英]busy wait threads in java
我有線程的任務,我不能讓線程進入忙等待狀態。 我如何知道線程是處於阻塞狀態還是處於繁忙等待狀態? 有沒有檢查它的命令?
在程序中我有2個矩陣,我需要轉換它們。 所以我有一個轉換線程,代碼如下:
transformThread transformThreadFirst = new transformThread(firstMat, n);
transformThread transformThreadSecond = new transformThread(secondMat, n);
transformThreadFirst.start();
transformThreadSecond.start();
try
{
transformThreadFirst.join();
transformThreadSecond.join();
}
catch(InterruptedException e)
{
}
任何線程都將處於忙碌狀態等待還是可以的? 或者你有更好的解決方案? 同樣在transformThread的運行中我不使用任何yield,只有2 for循環,就是它,只是轉換動作..
忙碌的等待是反復做的事情,直到另一個操作結束。 像這樣的東西:
// The next line will be called many many times
// before the other thread finishes
while (otherThread.getState() == Thread.State.RUNNABLE) {}
所以,你的代碼是不是忙等待,當前線程將被阻塞,直到transformThreadFirst完成,然后阻塞,直到transformThreadSecond完成。
有一個jstack實用程序。 將它用作jstack <pid>
,傳遞正在運行的jvm的進程ID。 它將顯示該應用程序中的每個線程正在執行的操作,以及線程狀態(RUNNING,WAITING,BLOCKED)。
它與JDK捆綁在一起並位於其bin /中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.