Java多线程学习-Thread.state 有哪些?
2014-04-08 09:37
113 查看
What is Thread.State in Java? What's it used for?
Thread.State - This is a static nested class (Read more about nested classes in the article - Nested
Classes & Inner Classes in Java >>) of the Thread class. This is one of the additions of Java 5 and this class actually inherits the abstract classEnum which
is the common base class of all Java language enumeration types i.e.,Thread.State is actually is actually an enumeration type.
Thread.State enumeration contains the possible states of a Java thread in the underlying JVM. These states are different from the Operating System
thread states. The possible values of the Thread.State are:-
NEW - this state represents a new thread which is not yet started.
RUNNABLE - this state represents a thread which is executing in the underlying JVM. Here executing in JVM doesn't mean that the thread is always executing in the OS as well
- it may wait for a resource from the Operating system like the processor while being in this state.
BLOCKED - this state represents a thread which has been blocked and is waiting for a moniotor to enter/re-enter a synchronized block/method. A thread gets into this state
after calling Object.wait method.
WAITING - this state represnts a thread in the waiting state and this wait is over only when some other thread performs some appropriate action. A thread can get into this
state either by calling - Object.wait (without timeout), Thread.join(without timeout), or LockSupport.park methods.
TIMED_WAITING - this state represents a thread which is required to wait at max for a specified time limit. A thread can get into this state by calling either of these methods: Thread.sleep, Object.wait (with
timeout specified),Thread.join (with timeout specified), LockSupport.parkNanos,LockSupport.parkUntil
TERMINATED - this state reprents a thread which has completed its execution either by returning from the run() method after
completing the execution OR by throwing an exception which propagated from the run() method and hence caused the termination of the thread.
Difference between BLOCKED state
and WAITING / TIMED_WAITING states?
When a thread calls Object.wait method,
it releases all the acquired monitors and is put into WAITING (or TIMED_WAITING if
we call the timeout versions of the waitmethod)
state. Now when the thread is notified either by notify() or
bynotifyAll() call
on the same object then the waiting state of the thread ends and the thread starts attempting to regain all the monitors which it had acquired at the time of wait call.
At one time there may be several threads trying to regain (or maybe gain for the first time) their monitors. If more than one threads attempt to acquire the monitor of a particular object then only one thread (selected by the JVM scheduler) is granted the
monitor and all other threads are put into BLOCKED state.
Got the difference?
Difference between WAITING and TIMED_WAITING states?
The difference is quite obvious between the two. A thread in a TIMED_WAITING state
will wait at max for the specified timeout period whereas a thread in the WAITING state
keeps waiting for an indefinite period
of time. For example, if a thread has calledObject.wait method
to put itself into WAITING state
then it'll keep waiting until the thread is interrupted either by notify() method
(OR by notifyAll() method)
call on the same object by another thread. Similarly, if a thread has put itself into WAITINGstate
by calling Thread.join method
then it'll keep waiting until the specified thread terminates.
We can easily figure out that a thread in a WAITING state will always be dependent on an action performed
by some other thread whereas a thread in TIMED_WAITING is not completely dependent on an action performed by some other thread as in this case the wait ends automatically after the completion of the timeout period.
转载地址:http://geekexplains.blogspot.com/2008/07/threadstate-in-java-blocked-vs-waiting.html
Thread.State - This is a static nested class (Read more about nested classes in the article - Nested
Classes & Inner Classes in Java >>) of the Thread class. This is one of the additions of Java 5 and this class actually inherits the abstract classEnum which
is the common base class of all Java language enumeration types i.e.,Thread.State is actually is actually an enumeration type.
Thread.State enumeration contains the possible states of a Java thread in the underlying JVM. These states are different from the Operating System
thread states. The possible values of the Thread.State are:-
NEW - this state represents a new thread which is not yet started.
RUNNABLE - this state represents a thread which is executing in the underlying JVM. Here executing in JVM doesn't mean that the thread is always executing in the OS as well
- it may wait for a resource from the Operating system like the processor while being in this state.
BLOCKED - this state represents a thread which has been blocked and is waiting for a moniotor to enter/re-enter a synchronized block/method. A thread gets into this state
after calling Object.wait method.
WAITING - this state represnts a thread in the waiting state and this wait is over only when some other thread performs some appropriate action. A thread can get into this
state either by calling - Object.wait (without timeout), Thread.join(without timeout), or LockSupport.park methods.
TIMED_WAITING - this state represents a thread which is required to wait at max for a specified time limit. A thread can get into this state by calling either of these methods: Thread.sleep, Object.wait (with
timeout specified),Thread.join (with timeout specified), LockSupport.parkNanos,LockSupport.parkUntil
TERMINATED - this state reprents a thread which has completed its execution either by returning from the run() method after
completing the execution OR by throwing an exception which propagated from the run() method and hence caused the termination of the thread.
Difference between BLOCKED state
and WAITING / TIMED_WAITING states?
When a thread calls Object.wait method,
it releases all the acquired monitors and is put into WAITING (or TIMED_WAITING if
we call the timeout versions of the waitmethod)
state. Now when the thread is notified either by notify() or
bynotifyAll() call
on the same object then the waiting state of the thread ends and the thread starts attempting to regain all the monitors which it had acquired at the time of wait call.
At one time there may be several threads trying to regain (or maybe gain for the first time) their monitors. If more than one threads attempt to acquire the monitor of a particular object then only one thread (selected by the JVM scheduler) is granted the
monitor and all other threads are put into BLOCKED state.
Got the difference?
Difference between WAITING and TIMED_WAITING states?
The difference is quite obvious between the two. A thread in a TIMED_WAITING state
will wait at max for the specified timeout period whereas a thread in the WAITING state
keeps waiting for an indefinite period
of time. For example, if a thread has calledObject.wait method
to put itself into WAITING state
then it'll keep waiting until the thread is interrupted either by notify() method
(OR by notifyAll() method)
call on the same object by another thread. Similarly, if a thread has put itself into WAITINGstate
by calling Thread.join method
then it'll keep waiting until the specified thread terminates.
We can easily figure out that a thread in a WAITING state will always be dependent on an action performed
by some other thread whereas a thread in TIMED_WAITING is not completely dependent on an action performed by some other thread as in this case the wait ends automatically after the completion of the timeout period.
转载地址:http://geekexplains.blogspot.com/2008/07/threadstate-in-java-blocked-vs-waiting.html
相关文章推荐
- MFC源码学习,为什么_AFX_THREAD_STATE *pState=_afxThreadState
- java多线程学习 (四) Thread的interrupt()[中断] {转载}
- Java多线程学习-Thread.state 解释
- [LWIP学习]--tcpip_input,tcpip_inpkt,tcpip_thread函数分析(协议栈入口)
- C# 多线程学习(六)线程池(ThreadPool)——线程资源的复用和自动管理
- 学习通过Thread+Handler实现非UI线程更新UI组件(转)
- java学习---java内存泄露有哪些
- python3 threading thread 模块学习
- 优先级反转实验,使用信号量实现【RT-Thread学习笔记 5】
- STM32 + RT Thread OS 学习笔记[七]
- Android 4 学习(15):持久化:Files, Saving State and Preferences
- 编程老手的哪些特点,是值得新手程序员学习的?
- Thread State and the Global Interpreter Lock
- Linux内核 kthread_run函数 理解学习
- 成为一名嵌入式Linux开发工程师需要学习哪些知识?
- 32.Android之消息处理系统Looper、Handler、Thread简单学习
- Thread多线程学习(二),java多线程中synchronize锁的使用和学习
- 设计模式深入学习--State 状态模式(行为型模式)
- 【React】哪些数据应该放到state中?
- 【04】 Boost库学习笔记之线程(thread)