本文目录一览:
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的,我没试过到底准不准,不过这个最省事了,右键点出来一看就知道了。