监控工具
Java VisualVM 是可视化的查看JVM运行信息的工具,位于 $JAVA_HOME/bin/jvisualvm
官方文档 https://docs.oracle.com/javase/8/docs/technotes/guides/visualvm/
在菜单工具-插件,安装 Threads Inspector 插件可以查看线程运行状态。
线程状态
Thead的内部枚举类定义了JVM层面的6种线程状态。
- NEW
Thread state for a thread which has not yet started.
未启动的线程状态。
- RUNNABLE
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.
线程在JVM 中可运行,执行权交给 OS。
- BLOCKED
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 Object.wait.
线程等在获取 monitor lock,两类情况:一个线程等待 monitor lock 来进入同步代码块或同步方法;一个线程执行到同步代码块或者方法中的 Object.wait() 方法后处于 WAITING 状态,当其他线程调用 Object.notifyAll() 后再次尝试进入(reenter)同步代码块或方法时等待 monitor lock。
- WAITING
Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:
-
- Object.wait with no timeout
- Thread.join with no timeout
- LockSupport.park
A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.
JVM层面的等待或者阻塞。
- TIMED_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:
- Thread.sleep
- Object.wait with timeout
- Thread.join with timeout
- LockSupport.parkNanos
- LockSupport.parkUntil
- TERMINATED
Thread state for a terminated thread. The thread has completed execution.
线程执行结束或者异常结束。
Blocked 和 Waiting 状态区别
可以简单理解线程为了对共享资源(同步代码块或者同步方法)的独占。
- 获取不到 monitor lock 时进入 block set 处于 Blocked 状态。
- 进入同步代码块后发现缺少某种资源,调用 Object.wait() 先释放共享资源(同步代码块或者同步方法)线程进入 wait set 处于 Waiting 状态,等待调用 Object.notifyAll() 呼唤自己出队,出队后重新尝试(reenter)获取共享资源(同步代码块或者同步方法)。
线程生命周期