编程语言
3481
Java Thread状态
NEW - 创建
被创建,但是还没有开始的线程,也就是还没有调用start()方法。
RUNNABLE - 运行就绪
可运行状态,在JVM中执行,但是可能等待操作系统CPU资源。
BLOCKED - 阻塞
线程阻塞等待获取监视器锁。 两种情况:
- 等待监视器锁以进入同步块/方法;
- 在调用完Object.wait方法后被唤醒,等待重新进入同步块/方法;
WAITING - 等待
等待状态,调用如下方法会导致进入等待状态:
- Object.wait ,无超时时间参数;
- Thread.join, 无超时时间参数;
- LockSupport.park
用于一个线程等待另外的线程去执行某些特殊动作。例如:一个线程调用Object.wait, 等待其他线程调用Object.notify() 或 Object.notifyAll() 来唤醒。一个线程调用Thread.join() 等待另一个线程终止。
TIMED_WAITING - 定时等待
带有一个指定的等待时间的等待状态,带一个指定的正的等待时间调用如下方法可导致进入定时等待状态:
- Thread.sleep
- Object.wait, 带超时时间参数
- Thread.join, 带超时时间参数
- LockSupport.parkNanos
- LockSupport.parkUntil
TERMINATED - 终止
终止状态,线程执行完成
线程状态程序演示
代码
public class TestThread implements Runnable { public static void main(String[] args) throws InterruptedException { TestThread tt = new TestThread(); Thread testThread = new Thread(tt); // 1. NEW - 创建testThread线程,未start System.out.printf("1. MAIN: %d - %s\n", testThread.getId(), testThread.getState().toString()); testThread.start(); Thread.sleep(1000L); // 3. TIMED_WAITING - testThread线程内部sleep 2秒 System.out.printf("3. MAIN: %d - %s\n", testThread.getId(), testThread.getState().toString()); tt.testBlock(testThread); Thread.sleep(2000L); // 6. WAITING - testThread线程中调用了tt.wait(); System.out.printf("6. MAIN: %d - %s\n", testThread.getId(), testThread.getState().toString()); tt.testNotify(testThread); Thread.sleep(2000L); // 9. TERMINATED - testThread执行完成,终止 System.out.printf("9. MAIN: %d - %s\n", testThread.getId(), testThread.getState().toString()); } @Override public void run() { Thread curThd = Thread.currentThread(); long tid = curThd.getId(); // 2. RUNNABLE - 开始执行 System.out.printf("2. ENTER: %d - %s\n", tid, curThd.getState().toString()); try { Thread.sleep(2000L); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (this) { // 5. RUNNABLE - 获取当前对象监视器锁 System.out.printf("5. enter sync block: %d - %s\n", tid, curThd.getState().toString()); try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } // 8. RUNNABLE - testThread获取到监视器锁,继续执行 System.out.printf("8. end sync block: %d - %s\n", tid, curThd.getState().toString()); } } public synchronized void testBlock(Thread testThread) { try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } // 4. BLOCKED - 当前线程获取了当前对象的监视器锁,testThread等待监视器锁中; System.out.printf("4. testBlock: %d - %s\n", testThread.getId(), testThread.getState().toString()); } public synchronized void testNotify(Thread testThread) throws InterruptedException { notify(); try { Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } // 7. BLOCKED - 调用了tt对象的notify方法,testThread的状态由WAITING变为BLOCKED, 之所以是BLOCKED,因为tt对象的监视器锁被当前线程获取。 System.out.printf("7. testNotify: %d - %s\n", testThread.getId(), testThread.getState().toString()); } }
输出
1. MAIN: 11 - NEW 2. ENTER: 11 - RUNNABLE 3. MAIN: 11 - TIMED_WAITING 4. testBlock: 11 - BLOCKED 5. enter sync block: 11 - RUNNABLE 6. MAIN: 11 - WAITING 7. testNotify: 11 - BLOCKED 8. end sync block: 11 - RUNNABLE 9. MAIN: 11 - TERMINATED
广告