Thread method Join () and sleep () difference

Source: Internet
Author: User

Description:

Method Join () is used to cause the thread object that belongs to execute the run () method normally, while the current thread z is blocked indefinitely, waiting for thread x to be destroyed before continuing with the code now following Z.

Join also has a join (long) method, that is, long to set the wait time,


1, join () and synchronized

The join () method has the function of keeping threads queued, some similar to synchronized,

the difference with synchronized is that viewing the source can see that the join method is internally using the Wait () method for waiting, while synchronized uses the object monitor principle to implement synchronization.
2. Join () and sleep ()
The thread also has a sleep (long) method in addition to the Join method that can also be implemented to wait;
the difference from sleep () is that because the inner implementation of the join IS Wait (), the Join () method releases the lock, so other threads can call the thread's synchronization method.
The sleep (long) method has a feature that is not a lock, so the thread waits until the task completes before releasing the lock.

Note : The join () method may have an exception if it encounters a thread's interrupt () method, for example: A thread waits for the B thread to perform the task, and the B thread executes to half after interrupt (), which is reported Java.lang.InterruptedException exception.

At this point a thread does not terminate or is in the normal running state.




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.