Java sleep (), join (), yield (), wait (), notify (), Notifyall () differences

Source: Internet
Author: User
Tags thread class

1. Sleep ()

Causes the current thread (that is, the thread that called the method) to suspend execution for a period of time, allowing other threads to continue executing, but it does not release the object lock. This means that if there is synchronized synchronization fast, other threads still cannot access the shared data. Note that the method is catching an exception.

For example, there are two threads executing at the same time (without synchronized) One thread priority is max_priority, the other is min_priority, and if there is no Sleep () method, the low-priority line friend can execute when the high-priority thread finishes executing But when the high-priority thread sleep (500), the low priority has a chance to execute.

In summary, sleep () allows a low-priority thread to get the chance to execute and, of course, allow the same priority, high-priority threads to execute.

2. Join ()

The join () method causes the thread that invokes the method to complete before the thread that waits for the method finishes executing. Note that this method also requires catching 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 allows the same priority thread to have an opportunity 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 the synchronized statement block. The Synchronized keyword is used to protect shared data and prevent other threads from accessing the shared data, but the process of the program is inflexible and how can other threads have access to shared data when the current thread has not exited the synchronized data block? At this point, use these three methods to control flexibly.

The wait () method suspends the current thread and releases the object lock, allowing other threads to enter the synchronized data block, where the current thread is placed in the object waiting pool. When the Notify () method is called, an arbitrary thread is removed from the object's wait pool and placed in the lock flag waiting pool, and only the lock flag waits for the thread to acquire the lock flag, and notify () does not work if there are no threads in the lock flag waiting pool.

Notifyall () removes all the threads waiting for that object from the object waiting 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 processed into the run () method, and the start () method will automatically call the run () method, which is defined 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, but the prerequisite is to distinguish which data is shared. Each object has a lock flag, and when a thread accesses the object, the synchronized-modified data is "locked" and blocked from being accessed by other threads. When the current thread finishes accessing this piece of data, the lock flag is released and other threads can access it.

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

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

void Wait () causes the current thread to wait before other threads call the Notify () method of this object or the Notifyall () method.

void wait (long timeout) causes the current thread to wait before another thread calls this object's notify () method or the Notifyall () method, or exceeds the specified amount of time.

After wait (), the thread releases the "lock flag" that it occupies, so that other shnchronized data in the object where the thread resides can be used by other threads.

Wait () H and notify () because they operate on the object's "lock flags," they must be called in the synchronized function or synchronized block. If the call is made in the non-synchronized function or non-synchronized block, the illegalmonitorstateexception exception will occur at run time, although it can be compiled.

(2), Thread.Sleep (long Millis) must have a time parameter.

Sleep (long) puts the current thread into a stagnant state, so the thread executing sleep () will not be executed for a specified period of time;

Sleep (long) allows the thread with the lower priority to be executed and, of course, the opportunity to execute with the same priority thread;

Sleep (long) does not release the lock flag.

(3), yield () no parameters

The Sleep method causes the currently running thread to be asleep for a period of time, entering the non-operational state, the length of time is set by the program, and the yield method allows the current thread to give up the CPU possession, but the time to make up is not settable.

Yield () also does not release the lock flag.

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

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

Yield () simply returns the current thread back to the executable state, and all threads that execute yield () are likely to be executed immediately after entering the executable state, so the yield () method only allows the same priority thread to have an opportunity to execute.

Java sleep (), join (), yield (), wait (), notify (), Notifyall () differences

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.