1. 查看NEW、RUNNABLE、TERMINATED
package thread.state;
public class NewRunnableTerminatedState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
System.out.println("测试runnable: " + Thread.currentThread().getState());
});
System.out.println("测试new:" + thread.getState());
thread.start();
Thread.sleep(1000);
System.out.println("测试terminated:" + thread.getState());
}
}
2. 查看BLOCKED状态
package thread.state;
public class BlockedState {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
try {
Thread.sleep(100);
synchronized(BlockedState.class){
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread thread2 = new Thread(() -> {
try {
Thread.sleep(200);
synchronized(BlockedState.class){
Thread.sleep(300);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread1.start();
thread2.start();
Thread.sleep(300);
System.out.println("测试blocked:" + thread2.getState());
}
}
3. 查看WAITING状态
package thread.state;
import java.util.concurrent.locks.LockSupport;
public class WaitingState {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(LockSupport::park);
Thread thread2 = new Thread(() -> {
try {
Thread.sleep(300);
LockSupport.unpark(thread1);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread1.start();
thread2.start();
Thread.sleep(100);
System.out.println("处于park状态:" + thread1.getState());
Thread thread3 = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread thread4 = new Thread(() -> {
try {
thread3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread3.start();
thread4.start();
Thread.sleep(500);
System.out.println("等待join完成:" + thread4.getState());
Object lock = new Object();
Thread thread5 = new Thread(() -> {
synchronized (lock){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread thread6 = new Thread(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock){
lock.notify();
}
});
thread5.start();
thread6.start();
Thread.sleep(300);
System.out.println("处于wait,等待被唤醒:" + thread5.getState());
}
}
4. 查看TIMED_WAITING状态
package thread.state;
public class TimeWaitingState {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread1.start();
Thread.sleep(100);
System.out.println("处于sleep状态:" + thread1.getState());
Object lock = new Object();
Thread thread2 = new Thread(() -> {
synchronized (lock){
try {
lock.wait(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread thread3 = new Thread(() -> {
try {
Thread.sleep(200);
synchronized (lock){
lock.notify();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread2.start();
thread3.start();
Thread.sleep(100);
System.out.println("处于wait有参数的状态:" + thread2.getState());
}
}