Java中线程状态
- NEW: 线程刚创建,还没有调用start()方法;
- RUNNABLE:线程准备就绪状(即:调用start()方法)或运行中;
- TIME_WAITING:等待隔一段时间自动唤醒;
- WAITING: 等待被唤醒;
- BLOCKED:阻塞,正在等待锁;
- TERMINATED:线程结束;
线程状态转换图

代码验证状态转换图
NEW、RUNNABLE、TERMINATED状态
public class ThreadStatusTest {
public static void main(String[] args) throws Exception {
Thread t1 = new Thread(() -> {
System.out.println("2、"+Thread.currentThread().getState());
});
System.out.println("1、"+t1.getState());
t1.start();
t1.join();
System.out.println("3、"+t1.getState());
}
}
TIMED_WAITING状态
public class ThreadTimedWaitingTest {
public static void main(String[] args) throws Exception {
Object o = new Object();
Thread t2 = new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(3);
synchronized (o) {
o.wait(3000);
}
Thread thread = new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread.start();
thread.join(2000);
LockSupport.parkNanos(2000000000);
LockSupport.parkUntil(System.currentTimeMillis() + 2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t2.start();
TimeUnit.SECONDS.sleep(1);
System.out.println("4、" + t2.getState());
TimeUnit.SECONDS.sleep(3);
System.out.println("5、" + t2.getState());
TimeUnit.SECONDS.sleep(3);
System.out.println("6、" + t2.getState());
TimeUnit.SECONDS.sleep(2);
System.out.println("7、" + t2.getState());
TimeUnit.SECONDS.sleep(1);
System.out.println("8、" + t2.getState());
}
}
WAITING状态
public class ThreadWaitingTest {
public static void main(String[] args) throws Exception {
Object o = new Object();
ReentrantLock lock = new ReentrantLock();
Thread t1 = new Thread(() -> {
synchronized (o) {
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Thread joinThread = new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
try {
joinThread.start();
joinThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
LockSupport.park();
Thread lockThread = new Thread(() -> {
lock.lock();
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
});
lockThread.start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.lock();
lock.unlock();
});
t1.start();
TimeUnit.SECONDS.sleep(1);
System.out.println("1、" + t1.getState());
synchronized (o) {
o.notify();
}
TimeUnit.SECONDS.sleep(1);
System.out.println("2、" + t1.getState());
TimeUnit.SECONDS.sleep(2);
System.out.println("3、" + t1.getState());
LockSupport.unpark(t1);
TimeUnit.SECONDS.sleep(2);
System.out.println("4、"+t1.getState());
}
}
BLOCKED
public class ThreadBlockedTest {
public static void main(String[] args) throws InterruptedException {
Object o = new Object();
Thread t1 = new Thread(() -> {
synchronized (o){
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(() -> {
synchronized (o){
}
});
t1.start();
TimeUnit.SECONDS.sleep(1);
t2.start();
TimeUnit.SECONDS.sleep(1);
System.out.println(t2.getState());
}
}