"One-minute knowledge" thread synchronization, thread scheduling

Source: Internet
Author: User
"One-minute knowledge" thread synchronization, thread scheduling


The difference between the sleep () and yield () methods of a thread

1

The sleep () method gives the other threads an opportunity to run without taking into account the priority of the thread, thus giving the lower-priority thread the chance of running;

The yield () method only gives the same priority or higher priority threads the opportunity to run;

2

Thread executes the sleep () method into a blocking (blocked) state,

The execution of the yield () method is then transferred to the Ready (ready) state;

3

The sleep () method declaration throws Interruptedexception,

The yield () method does not declare any exceptions;

4

The sleep () method is more portable than the yield () method (associated with operating system CPU scheduling).


Thread synchronization and thread scheduling-related methods

Wait ()

Causes a thread to be in a waiting (blocking) state and release the lock of the object being held;

Sleep ()

To make a running thread sleep, a static method that calls this method to handle the interruptedexception exception;

Notify ()

Waking a waiting state, of course, when calling this method, does not exactly wake up a waiting state of the thread, but rather by the JVM to determine which thread to wake, regardless of priority;

Notityall ()

Wakes all waiting-state threads, not locking objects to all threads, but letting them compete, and only the thread that gets the lock can enter the ready 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.