本文目錄一覽:
java獲取當前線程狀態。
java線程的狀態有下面幾種狀態:
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* ul
* li{@link Object#wait() Object.wait} with no timeout/li
* li{@link #join() Thread.join} with no timeout/li
* li{@link LockSupport#park() LockSupport.park}/li
* /ul
*
* pA thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called ttObject.wait()/tt
* on an object is waiting for another thread to call
* ttObject.notify()/tt or ttObject.notifyAll()/tt on
* that object. A thread that has called ttThread.join()/tt
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* ul
* li{@link #sleep Thread.sleep}/li
* li{@link Object#wait(long) Object.wait} with timeout/li
* li{@link #join(long) Thread.join} with timeout/li
* li{@link LockSupport#parkNanos LockSupport.parkNanos}/li
* li{@link LockSupport#parkUntil LockSupport.parkUntil}/li
* /ul
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
java多線程有哪些狀態?
初始態:一個線程調用了new方法之後,並在調用start方法之前的所處狀態。
就緒:一旦線程調用了start 方法,線程就轉到Runnable 狀態。
阻塞/ NonRunnable:線程處於阻塞/NonRunnable狀態,這是由兩種可能性造成的:要麼是因掛起而暫停的,要麼是由於某些原因而阻塞的,例如包括等待IO請求的完成。
停止/退出:線程轉到退出狀態,這有兩種可能性,要麼是run方法執行結束,要麼是調用了stop方法。
如何查看一個java進程有多少個線程在工作?
理論上來說,如果你全用threadgroup來跑線程的話,有個叫enumerate的方法可以得到【該threadgroup下】所有active的(也就是你說的在工作的)線程以及子線程,但程序裡面不一定是把線程放threadgroup裡面的,有可能有人自己new一個出來run或者別的,所以不能完全依靠這個辦法。我目前想到的辦法,貌似得弄個全局的counter,開個守護線程,讓這個線程去數監控所有線程的狀態,依照線程是否Active去加減這個counter。
還有個懶點的,你看windows的taskmanager(假設你在win平台上的話),裡面有一個column叫做thread count的,我沒試過到底準不準,不過這個最省事了,右鍵點出來一看就知道了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/160635.html