Sleep Join Yield | Wait Notify Notifyall

Source: Internet
Author: User

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 to execute, but itdo not release object locks. That is, if there are synchronized synchronization blocks, other threads still have different access to the shared data. Note that the method wants to catch exceptions

For example, there are two threads executing at the same time (no synchronized), one thread priority is max_priority, the other is min_priority, and if there is no sleep () method, a low-priority thread can execute only after the high-priority thread finishes executing But when the high-priority thread sleep (5000), the low priority has a chance to execute.

in short,sleep () allows a low-priority thread to get the chance to execute, of course, the same priority, high-priority threads have the opportunity to execute.

2.join ()

The join () method causes the thread that called the method to execute before this is done, that is, the thread that is waiting to invoke the method finishes execution and then proceeds down. Note that the method also catches exceptions.
wait for the thread that called the Join method to end, and then continue execution. such as: T.join ();//mainly used to wait for the T thread to run the end, without this sentence, main will be executed, resulting in unpredictable results.

3.yield ()  

cannot be specified by the user for how long to pause and the yield () method allows only threads with the same priority There is an opportunity for execution.  
yield () simply brings the current thread back to its executable state, so the thread that executes yield () is likely to be executed immediately after entering the executable state.


4.wait () and notify (), Notifyall ( )

These three methods are used to coordinate the access of multiple threads to shared data, so you must use these three methods within a synchronized statement block. I said synchronized. This keyword is used to protect shared data and prevent other threads from accessing shared data. However, the process of the program is very inflexible, how can other threads also 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 execution of the current thread andReleasing object Locksflag 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 called, an arbitrary thread is removed from the object's wait pool and placed in the lock flag waiting pool, only

The lock flag waits for a thread in the pool to acquire a lock flag, and notify () does not work if the lock flag waits for a thread in the 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 all three methods are Java.lang.Ojbect!

Sleep Join Yield | wait notify Notifyall

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.