Java Multi-thread Wait,sleep,yield,notify__java

Source: Internet
Author: User
Tags thread class

1. Sleep ()

Causes the current thread (that is, the thread that calls the method) to suspend execution for a period of time so that other threads have the opportunity to continue executing, but it does not release object locks. That is, if a synchronized synchronization is fast, other threads still cannot access the shared data. Note that the method will catch exceptions.

For example, there are two threads executing at the same time (without synchronized) One thread priority is max_priority, the other is min_priority, if there is no sleep () method, the lower-priority line Cheng can execute after the high priority thread has finished executing , but the high priority thread sleep (500) has the opportunity to execute with a low priority.

In short, sleep () can give low-priority threads an opportunity to execute, and, of course, give the same priority, high-priority threads an opportunity to execute.


2. Join ()

The join () method causes the thread that invoked the method to execute before this, that is, the thread that waits for the method finishes execution before continuing (blocking the current method). Note that the method also needs to catch exceptions.

3, yield ()

This method is similar to sleep (), except that it is not possible for the user to specify how long to pause, and the yield () method only gives the same priority thread the chance to execute.

4, Wait () and notify (), Notifyall ( )

These three methods are used to coordinate the access of multiple threads to shared data, so they must be used within a synchronized statement block. The Synchronized keyword is used to protect shared data and prevent other threads from accessing shared data, but the process of the program is so inflexible that it allows other threads to access the shared data when the current thread has not exited the synchronized data block. Now use these three methods to control the flexibility.

The wait () method causes the current thread to suspend execution and release the object lock mark so that other threads can enter the synchronized data block, and the current thread is placed in the object waiting pool. When the Notify () method is invoked, an arbitrary thread is removed from the waiting pool of the object and placed in the lock flag waiting pool, only the lock flag waits for the thread to get the lock flag, and notify () does not work if the object waits for no threads in the pool.

Notifyall () Removes all threads waiting for that object from the object wait pool and puts it in the lock flag waiting pool.

Note that these three methods are java.lang.Object methods.



second, run and start ()

Put the code that needs to be handled into the run () method, and the start () method starts the thread to automatically invoke the run () method, which is specified by the Java memory mechanism. and the run () method must be public access and the return value type is void.


third, the key word synchronized

This keyword is used to protect shared data, although the prerequisite is to distinguish between which data is shared data. Each object has a lock flag, and when a thread accesses the object, the data decorated by the synchronized is "locked" to prevent other threads from accessing it. When the current thread has finished accessing this part of the data, the lock flag is released and other threads are accessible.



The wait () and notify (), Notifyall () are methods of the object class, and sleep () and yield () are methods of the thread class.

(1), the commonly used wait method has wait () and wait (long timeout);

void Wait () results in the current thread waiting before other threads call the Notify () method of this object or the Notifyall () method.

void wait (long timeout) causes the current thread to await when other threads call the object's notify () method or the Notifyall () method, or before the specified amount of time.

After wait (), the thread frees up its "lock flag" so that other shnchronized data in the object where the thread resides can be used by another thread.


Wait () H and notify () are required to be invoked in the synchronized function or synchronized block because they operate on the lock flags of the objects. If invoked in the Non-synchronized function or in the non-synchronized block, the illegalmonitorstateexception exception will occur at run time, although it can be compiled.


(2), Thread.Sleep (long Millis) must be accompanied by a time parameter.

Sleep (long) causes the current thread to stagnate, so the thread that executes the sleep () is certainly not executed within the specified time;

Sleep (long) enables lower-priority threads to execute and, of course, gives the same-priority threads the opportunity to execute;

Sleep (long) does not release the lock flag.


(3), yield () No parameters

Sleep method so that the current running thread sleeping for a period of time, enter the state can not run, the length of time is set by the program, yield method to make the current thread out of the CPU possession, but the time is not set aside.

Yield () also does not release lock flags.

In fact, the yield () method corresponds to the following operations, first detect whether the current thread of the same priority is in the same operational state, if there is, the CPU's possession to the secondary thread, otherwise continue to run the original thread, so the yield () method is called "concession", It gives the opportunity to other threads of the same rank.


The Sleep method allows a lower-priority thread to get a chance to run, but when the yield () method executes, the current thread is still in a running state, so it is not possible for the lower-priority thread to get CPU possession at this point. In a running system, if the higher priority thread does not call the sleep method and is not blocked by I/O, then the lower priority thread can only wait for all higher-priority threads to run to the end before they have a chance to run.


Yield () simply brings the current thread back to the executable state, and all threads executing yield () may be executed immediately after entering the executable state, so the yield () method can only give the same priority thread the chance to execute.

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.