The wait and wake mechanism for thread series three threads

Source: Internet
Author: User
Tags instance method

In order to more efficiently deal with some short time slices, task-heavy tasks, we may often use multithreading. But in the multi-threaded environment, it is very easy to have thread concurrency problem, thread deadlock is a common concurrency problem. To avoid such problems, we use communication between threads, and waiting for a wake-up mechanism is a form of communication between threads.

The main ways to wait for the wake-up mechanism are:

Public final void Wait () throws Interruptedexception:
The current thread must have this object monitor. The thread releases ownership of this monitor and waits until other threads wake up by calling the Notify method, or the Notifyall method notifies the thread waiting on this object's monitor. The thread then waits to regain ownership of the monitor before it can continue execution.
Wait () also has two additional overloaded methods:
Wait (long timeout), wait (long Timeout,int Nanos)
Public final void Notify ()
waitmethod to wait on the object's monitor. Until the current thread discards the lock on this object, it can continue to execute the awakened thread. The awakened thread will compete in the usual manner with all other threads that are actively synchronizing on the object;
For example, a thread that wakes up does not have reliable privileges or disadvantages as the next thread that locks this object. This method should be called only by threads that are the owner of this object monitor. A thread can be the owner of this object monitor by one of the following three methods:
1. By executing the synchronous instance method of this object.
synchronizedThe body of the statement.
ClassA type of object that can be synchronized by executing the static method of the class. Only one thread can have a monitor for an object at a time.
Public final void Notifyall ():
waitmethod to wait on the object's monitor. Until the current thread discards the lock on this object, it can continue to execute the awakened thread. The awakened thread will compete in the usual manner with all other threads that are actively synchronizing on the object;
For example, a thread that wakes up does not have a reliable privilege or disadvantage as the next thread that locks this object. This method should be called only by threads that are the owner of this object monitor.
Need to note:
1. The lock object used by the thread can be any object, which can be seen from the wait method and the Notify method, because they are methods of the object class, and the object class is the parent class of all classes.
2. The "monitor" mentioned above refers to "lock object".
3. When the wait method is called, the thread enters the blocking state, frees up the resource occupied, frees the CPU to execute, and throws the Interruptedexception, which is the same as the sleep method. But in a different place, the Wati method also releases the lock, and the sleep method does not.
Five-state conversion diagram for threads:

The wait and wake mechanism for thread series three threads

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.