Java Basics Review (2) multithreading-related sleep () yield () wait () join () and other methods briefly describe

Source: Internet
Author: User

2015-10-23

Today's review is the multi-threaded part, for threading programming commonly used several methods to sort out and distinguish slightly.

1.join method

(Before always remember the Daze, re-read again, is basically clear the Orz)

The Join method belongs to the thread, which means that the specified thread is added to the current thread execution sequence, allowing the two threads that would have been alternately executed to be merged into a sequential execution thread. For example: Thread thread-a and thread thread-b are executed at the same time, and the join () method of the thread thread-a is called in Thread-b, and the result is that thread thread-a continues until the thread thread-b execution is complete.

Use:

Thread.Join (); This is the thread after which the threads thread has finished executing before continuing the execution of the statement

Or

Thread.Join (Millis); Wait for thread thread execution to finish, wait time to be customized, end of time or thread end to execute after statement

Note: The Join method is implemented internally through the wait method provided by object. This means that to implement Thread.Join, you must be able to get the object lock of thread object threads. Otherwise, the time Thread.Join (Millis) waits may not be predictable. (Attach code for join ()

1 /**2 * Waits at more <code>millis</code> milliseconds for this thread to3 * die.    A timeout of <code>0</code> means to wait forever. 4  */5 6  Public Final synchronized voidJoinLongMillis)throwsinterruptedexception {7     LongBase =System.currenttimemillis ();8     Longnow = 0;9 Ten     if(Millis < 0) { One         Throw NewIllegalArgumentException ("Timeout value is negative"); A     } -      -     if(Millis = = 0) { the          while(IsAlive ()) { -Wait (0); -         } -}Else { +          while(IsAlive ()) { -             LongDelay = Millis-Now ; +             if(Delay <= 0) { A                  Break; at             } - wait (delay); -now = System.currenttimemillis ()-Base; -         } -     } -}
implementation of join ()


2.sleep method

The Thread.Sleep (Millis) method is a static method of a thread class that forces the currently executing thread (the thread that invokes the method) to suspend execution for a period of time and to surrender the CPU possession. After the pause time is over, return to the operational state.

Sleep is a static method that is called by the local method to implement, against the thread that calls the Thread.Sleep (Millis) method.

Role: The opportunity for a low-priority thread to be executed.

Note:

①sleep (Millis) will automatically wake up when it expires, returning to the operational state, not the running state. That is to say, the sleep method does not guarantee that the thread sleep will start immediately after the execution;

② thread execution Sleep method does not release object locks, and if there are syncronized synchronization blocks, other threads still cannot access the shared data.

3.yeild method

The Thread.yield () method is also a static method of a thread class that pauses the currently executing thread object (running state-to-run state) and executes other same-priority threads. The goal is for other threads with the same priority to get a run opportunity, allowing appropriate rotation between threads of the same priority.

The yield () method corresponds to the following actions: first detect whether the thread with the same priority is in the same operational state, if so, give the CPU possession to the secondary thread, or continue running the original thread. That is, yield () only causes threads with the same priority to have an opportunity to execute.

However, there is no guarantee that yield () can be compromised in real-world use, because a compromised thread is likely to be re-selected by the thread scheduler.

4.wait () and sleep ()

①wait (), notify (), Notifyall () are all methods of the object class to coordinate the access of multiple threads to share data and must be used within a synchronized statement block.

void Wait () causes the current thread to enter the wait, before other threads call the Notify () method of this object or the Notifyall () method. After wait (), the thread frees up the "object lock" that it occupies, making other synchronized data in the object that the thread resides in can be used by other threads.

The wait () method associates the Notify ()/notifyall () method, because the lock flags of the objects are manipulated, so they must be called in a synchronized function or block of code. Otherwise, a illeglemonitorstateexception exception occurs at run time.

②sleep (), yield () is the method of the thread class.

Sleep (Millis) puts the current thread into a stagnant state without releasing the object lock.

A more detailed understanding of the Join method: http://uule.iteye.com/blog/1101994

Java Basics Review (2) multithreading-related sleep () yield () wait () join () for a brief description of the method

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.