JAVA thread state and Synchronized,wait,sleep,yield,notify,notifyall__java

Source: Internet
Author: User

There are several states of the Java thread:


1: Create state (new): Thread is new and does not call start

2: Ready state (Runnable): Also known as an executable state, the thread scheduler has not allocated CPU time slice execution to the thread when it is in a ready state when the thread's Start method is invoked.

3: Running state (Running): The thread scheduler allocates CPU time slices to execute thread code.

4: Blocking state (Blocked): The thread in the running process for some reason paused to run into the blocking state, only to meet the conditions after the ready state, get the CPU before it can enter the running state again.

The blocking situation is divided into three types:

A: Waiting for blocking (wait): Call the () method, which is used with synchroined, the thread enters the object waiting pool, releases the synchroined lock, and is in a blocking state. When there are other threads notify,notifyall the back line

Enters the lock identity wait pool, which goes into a synchronized blocking state.

B: Synchronous blocking: The thread needs to acquire a lock while it is running, but the lock is held by another thread, it enters the lock identity wait pool and is in a synchronized blocking state. After the thread acquires the lock, the thread enters the ready state.

C: Other blocking: When thread sleep, or join, or I/O requests, know yield time, sleep time, join thread execution, or I/O return, thread into ready state.

5: State of Death (Dead): When the thread run method exits or the exception thread stops running, the thread dies.


There are two uses of synchronized,

One is written before the method, if the method is a static method, the acquired lock is a class lock, and the method of multithreading calling all instances of the class is mutually exclusive. If the method is not static, the acquired lock is an object lock, and the method of multithreading calling the same instance is mutually exclusive, and calling a different instance is not mutually exclusive.

One is the synchronized code block: synchronized () {//...}, as well as whether the method is a static method to distinguish between getting object locks or class locks.


Wait: Used with synchronized, you must want to obtain the specified lock lock to lock.wait. When wait, the lock is released, the thread enters the object-waiting pool, releases the synchroined lock, and is in a blocking state. When there are other threads notify,notifyall the back line into the lock identification wait pool, that is, into a synchronized blocking state.


Notify: In conjunction with synchronized, you must want to get the lock lock you specify in order to release the lock after the end of the code block, notifying a thread that has entered the waiting block because of lock entry into a synchronized blocking state.

Notifyall: In conjunction with synchronized, you must want to get the lock lock you specify in order to release the lock after the end of the code block, notifying all threads that have entered the waiting block because of lock entry into the synchronized blocking state.

Sleep: The thread pauses, enters a blocking state, but does not release the previously held lock, which allows the lower-priority thread to run.

Yield: The thread enters the ready state and does not release the lock, giving the other threads of the same priority a chance to run. But the next time you may continue to allocate CPU time slices, into the running state.

Join: Let one thread B join the tail of a thread A, and B cannot run until a runs out.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.