@Test
public void testThreadState1() throws Exception {
Thread thread4TestState = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1" + "-" + Thread.currentThread().getName() + "的状态是:" + Thread.currentThread().getState());
}
},"thread4TestState");
System.out.println("2" + "-" + thread4TestState.getName() + "的状态是:" + thread.getState());
thread.start();
System.out.println("3" + "-" + thread4TestState.getName() + "的状态是:" + thread.getState());
Thread.sleep(2000);
System.out.println("4" + "-" + thread4TestState.getName() + "的状态是:" + thread.getState());
System.in.read();
}
上面的输出是:
-
new
一个新的线程,没有调用start()
;输出:2-thread4TestState的状态是:NEW
-
调用
start()
;输出:3-thread4TestState的状态是:RUNNABLE
-
在
run()
方法里;输出:1-thread4TestState的状态是:RUNNABLE
-
run()
执行完了;输出:4-thread4TestState的状态是:TERMINATED
@Test
public void testThreadState2() throws Exception {
Object lock = new Object();
Thread thread4TestState1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
System.out.println("1" + "-" + Thread.currentThread().getName() + "获取到了锁,当前的状态是:" + Thread.currentThread().getState());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("2" + "-" + Thread.currentThread().getName() + "释放了锁,当前的状态是:" + Thread.currentThread().getState());
}
}, "thread4TestState1");
Thread thread4TestState2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
System.out.println("3" + "-" + Thread.currentThread().getName() + "获取到了锁,当前的状态是:" + Thread.currentThread().getState());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("4" + "-" + Thread.currentThread().getName() + "释放了锁,当前的状态是:" + Thread.currentThread().getState());
}
}, "thread4TestState2");
thread4TestState1.start();
thread4TestState2.start();
Thread.sleep(500);
System.out.println("5" + "-" + thread4TestState1.getName() + "当前的状态是:" + thread4TestState1.getState());
System.out.println("6" + "-" + thread4TestState2.getName() + "当前的状态是:" + thread4TestState2.getState());
System.in.read();
}
上面的输出是:
-
首先
thread4TestState1
获取到锁,进入同步代码块;输出:1-thread4TestState1获取到了锁,当前的状态是:RUNNABLE
-
然后,
thread4TestState1
睡1000ms,那在500ms时查看,由于调用了
sleep()
thread4TestState1
的状态就是TIMED_WAITING
;输出:5-thread4TestState1当前的状态是:TIMED_WAITING
-
而
thread4TestState2
由于thread4TestState1
还没睡醒,所以还没释放锁,那thread4TestState2
也就还在等待获取锁
,所以状态就是BLOCKED
输出:6-thread4TestState2当前的状态是:BLOCKED
-
1000ms过去了,
thread4TestState1
睡醒了,释放了锁,状态还是RUNNABLE
输出:2-thread4TestState1释放了锁,当前的状态是:RUNNABLE
-
由于
thread4TestState1
释放了锁,那thread4TestState2
就可以拿到锁,状态从BLOCKED
变为RUNNABLE
输出:3-thread4TestState2获取到了锁,当前的状态是:RUNNABLE
输出:4-thread4TestState2释放了锁,当前的状态是:RUNNABLE
@Test
public void testThreadState3() throws Exception {
Object lock = new Object();
Thread thread4TestState1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
System.out.println("1" + "-" + Thread.currentThread().getName() + "获取到了锁,当前的状态是:" + Thread.currentThread().getState());
try {
lock.wait(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("2" + "-" + Thread.currentThread().getName() + "即将释放锁,当前的状态是:" + Thread.currentThread().getState());
}
}
}, "thread4TestState1");
thread4TestState1.start();
Thread.sleep(300);
System.out.println("3" + "-" + thread4TestState1.getName() + "当前的状态是:" + thread4TestState1.getState());
System.in.read();
}
上面的输出是:
-
首先获取到了锁
输出:1-thread4TestState1获取到了锁,当前的状态是:RUNNABLE
-
获取到锁的线程调用了
wait(timeout)
输出:3-thread4TestState1当前的状态是:TIMED_WAITING
-
线程从
TIMED_WATING
状态恢复输出:2-thread4TestState1即将释放锁,当前的状态是:RUNNABLE
@Test
public void testThreadState3() throws Exception {
Object lock = new Object();
Thread thread4TestState1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
System.out.println("1" + "-" + Thread.currentThread().getName() + "获取到了锁,当前的状态是:" + Thread.currentThread().getState());
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("2" + "-" + Thread.currentThread().getName() + "即将释放锁,当前的状态是:" + Thread.currentThread().getState());
}
}
}, "thread4TestState1");
thread4TestState1.start();
Thread.sleep(50);
System.out.println("3" + "-" + thread4TestState1.getName() + "当前的状态是:" + thread4TestState1.getState());
synchronized (lock){
lock.notify();
}
System.out.println("4" + "-" + thread4TestState1.getName() + "当前的状态是:" + thread4TestState1.getState());
System.in.read();
}
上面的输出是:
-
首先获取到了锁
输出:1-thread4TestState1获取到了锁,当前的状态是:
RUNNABLE
-
线程调用了无参的
wait()
输出:3-thread4TestState1当前的状态是:
WAITING
-
主线程执行了
notify
操作,唤醒处于WAITING
的线程,输出有可能是RUNNABLE
或者BLOCKED
输出:4-thread4TestState1当前的状态是:
RUNNABLE
或者BLOCKED
-
输出:2-thread4TestState1即将释放锁,当前的状态是:
RUNNABLE