What is difference between sleep() method and yield() method of multi threading?

前端 未结 12 1726
野的像风
野的像风 2020-12-22 17:02

As currently executing thread while it encounters the call sleep() then thread moves immediately into sleeping stat. Whereas for yield() thread moves into runnable

相关标签:
12条回答
  • 2020-12-22 17:11

    One way to request the current thread to relinquish CPU so that other threads can get a chance to execute is to use yield in Java.

    yield is a static method. It doesn't say which other thread will get the CPU. It is possible for the same thread to get back the CPU and start its execution again.

    public class Solution9  {
    
    public static void main(String[] args) {
            yclass yy = new yclass ();
            Thread t1= new Thread(yy);
            t1.start();
            for (int i = 0; i <3; i++) {
                Thread.yield();
                System.out.println("during yield control => " + Thread.currentThread().getName());
            }
        }
    }
    
    class yclass implements Runnable{
    
        @Override
        public void run() {
            for (int i = 0; i < 3; i++) {
                System.out.println("control => " + Thread.currentThread().getName());
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-22 17:13

    Sleep causes thread to suspend itself for x milliseconds while yield suspends the thread and immediately moves it to the ready queue (the queue which the CPU uses to run threads).

    0 讨论(0)
  • 2020-12-22 17:13

    sleep() causes the thread to definitely stop executing for a given amount of time; if no other thread or process needs to be run, the CPU will be idle (and probably enter a power saving mode).

    yield() basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should. Otherwise, the current thread will continue to run.

    0 讨论(0)
  • 2020-12-22 17:19

    We can prevent a thread from execution by using any of the 3 methods of Thread class:

    1. yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority or higher priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The yielded thread when it will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent.

    2. join() If any executing thread t1 calls join() on t2 (i.e. t2.join()) immediately t1 will enter into waiting state until t2 completes its execution.

    3. sleep() Based on our requirement we can make a thread to be in sleeping state for a specified period of time (hope not much explanation required for our favorite method).

    0 讨论(0)
  • 2020-12-22 17:19

    Yield(): method will stop the currently executing thread and give a chance to another thread of same priority which are waiting in queue. If thier is no thread then current thread will continue to execute. CPU will never be in ideal state.

    Sleep(): method will stop the thread for particular time (time will be given in milisecond). If this is single thread which is running then CPU will be in ideal state at that period of time.

    Both are static menthod.

    0 讨论(0)
  • 2020-12-22 17:24

    Yield : will make thread to wait for the currently executing thread and the thread which has called yield() will attaches itself at the end of the thread execution. The thread which call yield() will be in Blocked state till its turn.

    Sleep : will cause the thread to sleep in sleep mode for span of time mentioned in arguments.

    Join : t1 and t2 are two threads , t2.join() is called then t1 enters into wait state until t2 completes execution. Then t1 will into runnable state then our specialist JVM thread scheduler will pick t1 based on criteria's.

    0 讨论(0)
提交回复
热议问题