Thread collaboration Methods: sleep, yield, wait, join, and sleepyield

Source: Internet
Author: User

Thread collaboration Methods: sleep, yield, wait, join, and sleepyield
 Sleep) 

  • When the t Thread calls Thread. sleep (), the Thread t will sleep for a specified time.
  • Thread. sleep () and Thread. currentThread (). sleep () have the same effect.
  • The sleep method does not release the synchronization lock.
  • Sleep does not affect other threads.
Yield (concession)
  • When Thread. yield () is called in the t Thread, the t Thread will discard the cpu scheduling (usually the current scheduling)
  • Yield allows the current thread to give in to other threads for execution
Wait (Lock wait)
  • When the t thread calls obj. when wait (), the t thread will enter the waiting state and discard the obj object lock until other threads call obj. Y ()/notifyAll () method to wake up thread t
  • The wait method releases the synchronization lock.
  • The thread can only call the wait method in one synchronization block.
  • The wait method allows two threads to run alternately.
Join)
  • When the main thread calls the t. join () thread, the main thread will wait until the t thread is fully executed.
  • Join allows two threads to be executed in serial mode

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.