package com.sunld;import java.lang.management.ManagementFactory;import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;/**
* @Title: TestMainThread.java
* @Package com.sunld
* Description:
* @author sunld
* @version V1.0.0
* CreateDate:2017年9月28日 下午3:54:19
*/public class TestMainThread {
public static void main(String[] args) { // 获取Java线程管理MXBean
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); // 不需要获取同步的monitor和synchronizer信息,仅获取线程和线程堆栈信息
ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false); // 遍历线程信息,仅打印线程ID和线程名称信息
for (ThreadInfo threadInfo : threadInfos) {
System.out.println("[" + threadInfo.getThreadId() + "] " +
threadInfo.getThreadName());
}
}
}[5] Attach Listener//附加监听
[4] Signal Dispatcher//分发处理发送给JVM信号的线程
[3] Finalizer//调用对象finalize方法的线程
[2] Reference Handler清除Reference的线程
[1] mainmain线程,用户程序入口 /**
* A thread state. A thread can be in one of the following states:
*
* - {@link #NEW}
* A thread that has not yet started is in this state.
*
* - {@link #RUNNABLE}
* A thread executing in the Java virtual machine is in this state.
*
* - {@link #BLOCKED}
* A thread that is blocked waiting for a monitor lock
* is in this state.
*
* - {@link #WAITING}
* A thread that is waiting indefinitely for another thread to
* perform a particular action is in this state.
*
* - {@link #TIMED_WAITING}
* A thread that is waiting for another thread to perform an action
* for up to a specified waiting time is in this state.
*
* - {@link #TERMINATED}
* A thread that has exited is in this state.
*
*
*
*
* A thread can be in only one state at a given point in time.
* These states are virtual machine states which do not reflect
* any operating system thread states.
*
* @since 1.5
* @see #getState
*/
public enum State {
/**
* 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:
*
* - {@link Object#wait() Object.wait} with no timeout
* - {@link #join() Thread.join} with no timeout
* - {@link LockSupport#park() 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.
*/
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:
*
* - {@link #sleep Thread.sleep}
* - {@link Object#wait(long) Object.wait} with timeout
* - {@link #join(long) Thread.join} with timeout
* - {@link LockSupport#parkNanos LockSupport.parkNanos}
* - {@link LockSupport#parkUntil LockSupport.parkUntil}
*
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}package com.sunld;
import java.util.concurrent.TimeUnit;/**
* @Title: TestThreadState.java
* @Package com.sunld
* Description:
* @author sunld
* @version V1.0.0
* CreateDate:2017年9月28日 下午5:14:27
*/public class TestThreadState {
public static void main(String[] args) {
new Thread(new TimeWaiting (), "TimeWaitingThread").start();
new Thread(new Waiting(), "WaitingThread").start(); // 使用两个Blocked线程,一个获取锁成功,另一个被阻塞
new Thread(new Blocked(), "BlockedThread-1").start();
new Thread(new Blocked(), "BlockedThread-2").start();
} //该线程不断地进行睡眠
static class TimeWaiting implements Runnable{
@Override
public void run() {
SleepUtils.second(100);
}
} //该线程在Waiting.class实例上等待
static class Waiting implements Runnable{
@Override
public void run() {
while (true) {
synchronized (Waiting.class) {
try {
Waiting.class.wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
} //该线程在Blocked.class实例上加锁后,不会释放该锁
static class Blocked implements Runnable {
@Override public void run() {
synchronized (Blocked.class) {
while (true) {
SleepUtils.second(100);
}
}
}
}
}class SleepUtils{
public static final void second(long seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}2017-09-28 17:26:47Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.112-b15 mixed mode):
//BlockedThread-2线程获取到了Blocked.class的锁"BlockedThread-2" #13 prio=5 os_prio=0 tid=0x000000001f268000 nid=0x3754 waiting on condition [0x000000002009f000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
//BlockedThread-1线程阻塞在获取Blocked.class示例的锁上"BlockedThread-1" #12 prio=5 os_prio=0 tid=0x000000001f266800 nid=0x89c waiting for monitor entry [0x000000001ff9f000]
java.lang.Thread.State: BLOCKED (on object monitor)
//WaitingThread线程在Waiting实例上等待"WaitingThread" #11 prio=5 os_prio=0 tid=0x000000001f260800 nid=0x4d08 in Object.wait() [0x000000001fe9f000]
java.lang.Thread.State: WAITING (on object monitor)
//TimeWaitingThread线程处于超时等待"TimeWaitingThread" #10 prio=5 os_prio=0 tid=0x000000001f25f000 nid=0x42ac waiting on condition [0x000000001fd9e000]
java.lang.Thread.State: TIMED_WAITING (sleeping)