Thread sleeping (Sleep) thread compromise (yield)

Source: Internet
Author: User
Tags thread class

Thread Sleep: Sleeping
(1) If you need to suspend the currently executing thread for a period of time and go into a blocking state, you can do so by calling the static method sleep () of the thread class, which has two overloaded forms.
static void sleep (Long Milis): Allow the currently executing thread to pause Millis milliseconds and enter a blocking state
static void sleep (Long milis,int Nanos) allows the currently executing thread to pause Millis milliseconds plus nanos microseconds and into a blocking state

Program instance: The following program calls the sleep () method to suspend execution of the main thread, because the program has only one main thread, when the main thread into insomnia, the system does not have an executable thread, so you can see the program at Sleep () stop
Import Java.util.Date;
public class sleeptest{
public static void Main (string[] args)
Throws Exception
{
for (int i = 0; i < i++) {
System.out.println ("Current time:" +new Date ());

            Call the Sleep () method to have the current thread pause 2s
            thread.sleep;
       }
 

}

————————————————————————————————————
Thread Concession: Yield
(1) The yield method is a somewhat similar method to the sleep () method, and it also allows the currently executing thread to pause, but it does not block the thread, it simply moves the thread into a ready state. The yield () method simply lets the current thread pause, allowing the system's thread scheduler to be dispatched again. After a thread has called the yield () method paused, only the priority is the same as the current thread, or a thread with a higher priority than the current thread is given an opportunity to execute: program instance:
public class Yieldtest extends Thread {
Public yieldtest (String name) {
Super (name);
}

Define the Run method as the thread executor public  void  run (
       int i = 0; i < i++) {
            System.out.println (getName () + ""  +i);
            When I equals 20 o'clock, use the yield () method to make the current thread compromise
            if (i ==20) {
                 Thread.yield ();
            }

 }} public static void Main (string[] args) {
       //starts two concurrent threads
       yieldtest   yt1 =new  yieldtest ("advanced");
       Set YT1 to highest priority
 //   yt1.setpriority (thread.max_priority);
       Yt1.start ();

       Yieldtest  yt2 =new yieldtest ("low Level");
       Set the YT2 to the lowest priority
 //   yt2.setpriority (thread.min_priority);
       Yt2.start ();
 }

}

With annotations, two threads have the same priority, so when one thread yield (), another thread executes.
After the annotation is canceled, the high priority thread calls the yield () method paused, the system does not have the same priority or higher thread, so the thread continues execution.

————————————————————————————
The difference between sleep () and yield () methods is as follows:
When the sleep () method pauses the current thread, it gives other threads an opportunity to ignore the priority of the other thread, but the yield () method only gives the same priority, or higher priority thread execution opportunities.
The sleep () method turns the thread into a blocking state until it is blocked until it is ready, and yield () does not turn the thread into a blocking state, he simply forces the current thread into a ready state. Therefore, it is entirely possible that after a thread calls the yield () method paused, it immediately obtains the processor resources to be executed.
The sleep declaration throws a interruptedexception exception, so either the exception is caught when the sleep () method is invoked, or the exception is explicitly declared, while the yield () method does not declare that it throws any exceptions.
The sleep () method is more portable than the yield () method, and it is generally not recommended to use the yield () method to control the execution of concurrent threads.

——————————————————————————————————————————————

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.