假设有线程Thread t
情况1:NEW–>RUNNABLE
当调用t.start()方法,NEW–>RUNNABLE
情况2:RUNNABLE<–>WAITING
t线程使用synchronized(obj)获取了对象锁之后
->调用obj.wait(),线程从RUNNABLE–>WAITING
->调用obj.notify(),obj.notifyAll(),t.interrupt()
->>竞争锁失败,WAITING->BLOCKED
->>竞争锁成功,WAITING->RUNNABLE
情况3:RUNNABLE<–>WAITING
1.当前线程调用t.join()方法时,当前线程从RUNNABLE–>WAITING,注意是当前线程在t线程对象的监视器上等待。
2.t线程运行结束之后,或调用了当前线程的interrupt()时,当前线程从RUNNABLE<–WAITING
情况4:RUNNABLE<–>WAITING
1.当前线程调用LockSupport.park()方法会让当前线程从RUNNABLE–>WAITING
2.调用LockSupport.unpark(目标线程)或调用了线程interrupt(),会让目标线程从RUNNABLE<–WAITING
情况5:RUNNABLE<–>TIMED_WAITING
1.t线程用synchronized(obj)获取了对象锁之后,调用obj.wait(long n)方法时,t线程从RUNNABLE–>TIMED_WAITING
2.t线程等待时间超过了n毫秒,或者调用obj.notify()、obj.notifyAll()、t.interrupt()时
->>竞争锁失败,TIMED_WAITING->BLOCKED
->>竞争锁成功,TIMED_WAITING->RUNNABLE
情况6:RUNNABLE<–>TIMED_WAITING
1.当前线程调用t.join(n)方法时,当前线程从RUNNABLE->TIMED_WAITING,注意时当前线程在t线程对象的监视器上等待
2.当前线程等待时间超过了n毫秒,或者t线程运行结束,或者调用了当前线程interrupt()方法时,当前线程从TIMED_WAITING->RUNNABLE
情况7:RUNNABLE<–>TIMED_WAITING
1.当前线程调用Thread.sleep(long n),当前线程从RUNNABLE->TIMED_WAITING
2.当前线程等待时间超过了n毫秒,当前线程从TIMED_WAITING->RUNNABLE
情况8:RUNNABLE<–>TIMED_WAITING
1.当前线程调用LockSupport.parkNanos(long nanos)或者LockSupport.parkUntil(long millis)时,当前线程从RUNNABLE–>TIMED_WAITING
2.调用LockSupport.unpark(目标线程)或者调用了线程的interrupt(),或者等待超时,会让目标线程从TIMED_WAITING–>RUNNABLE
情况9:RUNNABLE<–>BLOCKED
1.t线程用synchronized(obj)获取了对象锁时如果竞争失败,从RUNNABLE->>BLOCKED
2.持obj锁线程的同步代码块执行完毕,会唤醒该对象上所有BLOCKED的线程重新竞争,如果t线程竞争成功,从BLOCKED->RUNNABLE,失败的线程仍然是BLOCKED
情况10:RUNNABLE<–>TERMINATED
当前线程所有代码执行完毕,进入TERMINATED