Thread Operation wait and notify

Source: Internet
Author: User
Tags object object

After the thread has entered the running state, it can be changed to "wait for blocking" based on conditional triggering:

The running thread executes the wait () method, which releases all the resources it occupies, and the JVM puts the thread into the waiting pool. After entering this state, it is not automatically awakened, and must rely on other threads to invoke the Notify () or Notifyall () method to be awakened.


The following code:

1. Define two test thread classes

public class WaitTest1 extends Thread {
	private Object look;

	Public WaitTest1 (Object look) {
		This.look = look at
	;

	} @Override public
	Void Run () {
		try {
			synchronized (look) {
				System.out.println] ("Wait start ...");
				Look.wait ();
				SYSTEM.OUT.PRINTLN ("Wait-end ...");
			}
		catch (Exception e) {
			e.printstacktrace ();

}}} public class WaitTest2 extends Thread {

	private Object look;

	Public WaitTest2 (Object look) {
		This.look = look at
	;

	} @Override public
	Void Run () {
		try {
			synchronized (look) {
				System.out.println ("Notify start ...") C27/>look.notify ();
				System.out.println ("Notify end ...");
			}
		catch (Exception e) {
			e.printstacktrace ();}}}


2. Test class

public class Waitmain {public

	static void Main (string[] args) {
		try {
			Object look = "abc";
			WaitTest1 thread1 = new WaitTest1 (look);
			WaitTest2 thread2 = new WaitTest2 (look);

			Thread1.start ();
			Two seconds to wake up
			thread.sleep ();
			Thread2.start ();
		} catch (Exception e) {
			e.printstacktrace ();}}}


The code above implements the thread's wait-and-wake notify, after which the output is run:

Wait for start ...
Wait-end ...
Notify start ...
Notify end ...


Description:

You must obtain an object-level lock on the object before executing wait () or notify (), that is, you can call both methods only in the synchronization method or in the synchronization block.

The keyword synchronized can treat any object object as a synchronization object, and Java implements the Wait () and notify () method for each object.
They must be used in the object critical area that is synchronized synchronized. By calling the wait () method, the thread in the critical section can enter the waiting state while releasing the lock of the Synchronized object.
The notify operation wakes up a thread that is blocked by calling a wait operation. To get it in the Ready state (note: A ready state, not a running state), the awakened thread attempts to regain control of the critical section, that is, the lock, and continue to execute the code after the wait in the critical section.
The command is ignored if the notify operation is not a blocked thread.

The wait () method enables the thread that calls the method to release the lock on the shared resource, and then launches from the run state into the waiting queue until it is awakened again.
The notify () method can randomly wake the "one" thread waiting in the queue to wait for the shared resource and cause the thread to exit the wait queue and enter the operational state, that is, the Notify () method notifies only the "one" thread.
The Notifyall () method allows all threads waiting in the queue to wait for the shared resource to exit from the wait state and into a running state. At this point the highest priority thread executes first, but it can also be executed randomly because it depends on the implementation of the JVM virtual machine.

Note: It is important to note that when the method notify () is executed, the lock is not freed and the lock must be released after the synchronized synchronized code block in which the Notify () method is executed.

By the way, attach the thread state conversion diagram:







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.