Java线程基础
Java线程基础
线程状态
关于java的线程状态,实际上你只要看
Thread.java
源码就可以了,网上很多资料都不全,而且往往你看了资料以后根本记不住,反而自己去花时间看看源码,思考每次位运算的结果,才能让你真的理解和掌握线程的状态转换~当然,我也不建议你看我的这篇文章,毕竟我是写给我自己看的。
NEW
Thread state for a thread which has not yet started.
还没有开始的状态,就是new
RUNNABLE
Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual achine but it may be waiting for other resources from the operating system such as processor.
正在jvm虚拟机中运行的状态,但是可能还需要等待系统资源,类似于cpu这种资源。
BLOCKED
Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling
在monitor管程阻塞等待获取资源(锁)的状态,主要表现在synchronized block/method
WAITING
Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:
Object.wait with no timeout
Thread.join with no timeout
LockSupport.park
A thread in the waiting state is waiting for another thread to perform a particular action.
当调用Object.wait()
、Thread.join()
、LockSupport.park()
方法的时候,调用线程就会进入waiting状态,只有当线程调用Object.notify()
或者Object.notifyAll()
方法的时候才会终止。另外,一个线程调用另外一个线程的join()
方法,则该线程将等待另外一个线程执行结束。
TIMED_WAITING
Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time.
TERMINATED
Thread state for a terminated thread.The thread has completed execution.