本文用于记录学习黑马JUC课程后,个人认为有价值的笔记,核心内容均围绕标题展开。既做一个学习的记录,同时也做一个沟通交流,欢迎各位大佬互动~
New-->RUNNABLE
调用t.start()方法时,由New-->RUNNABLE,Java线程对象关联操作系统线程;
RUNNABLE<-->WAITING(obj.wait, obj.notify, obj.notifyAll, t.interrupt)
t线程用synchronized(obj)获得对象锁后,
- 调用obj.wait方法,t线程从RUNNABLE-->WAITING;
- 调用obj.notify,obj.notifyAll,t.interupt时,
- 锁竞争成功, t线程WAITING-->RUNNABLE;
- 锁竞争失败, t线程WAITING-->BLOCKED;
RUNNABLE<-->WAITING(join)
- 调用t.join(), RUNNABLE-->WAITING
- 注意:当前线程在t线程的监视器上等待
- t线程运行结束/调用当前线程的interrupt方法,从 WAITING-->RUNNABLE
RUNNABLE<-->WAITING(park, unpark)
- 当前线程调用LockSupport.park()方法, 会让当前线程从RUNNABLE-->WAITING
- 调用LockSupport.unpark(线程对象)或调用了线程的interrupt(),会让目标线程从WAITING-->RUNNABLE
RUNNABLE<-->TIMED_WAITING(obj.wait(long), obj.notify, obj.notifyAll, t.interrupt)
与2类似, 仅仅多个了超时转换状态的条件
RUNNABLE<-->TIMED_WAITING(join(long n))
与3类似, 仅仅多个了超时转换状态的条件
RUNNABLE<-->TIMED_WAITING(Thread.sleep(long n))
- 当前线程调用Thread.sleep(long n), 当前线程从RUNNABLE-->TIMED_WAITING;
- 当前线程等待时间超过n毫秒, 当前线程从TIMED_WAITING-->RUNNABLE
RUNNABLE<-->TIMED_WAITING(park(long n))
与4类似, 仅仅多个了超时转换状态的条件
RUNNABLE<-->BLOCKED
- t线程用synchronized(obj)获取了对象锁时如果竞争失败, 从RUNNABLE-->BLOCKED
- 持obj锁线程的同步代码块执行完毕, 会唤醒该对象上所有BLOCKED的线程重新竞争,如果其他t线程竞争成功,从BLOCKED-->RUNNABLE,其它失败的线程仍然BLOCKED.
RUNNABLE-->TERMINATED
当前线程所有代码运行完毕,进入TERMINATED