71

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

1
  • 1
    Rule of thumb: 1) never use yield(); 2) use sleep() only when the program is explicitly required to wait for the specified amount of time (if you only need to "wait a little bit", most chances are that you are doing it wrong and you need to implement a proper waiting/signaling mechanism).
    – Ilia K.
    Mar 14, 2012 at 13:04

12 Answers 12

155

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).

1
  • 4
    Also waiting on some synchronisation object such as a mutex.
    – Nick
    Mar 14, 2012 at 11:25
113

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.

7
  • as sleep method doesn't release the lock, so does it mean that no other thread can execute in that given object.
    – legend
    Aug 24, 2018 at 13:19
  • @legend: what lock? Neither sleep() nor yield() are in any way involved with synchronization or locks. You may be confusing them with Object.wait(). Aug 24, 2018 at 14:03
  • I come across following tow points while reading difference between wait() and sleep(). wait(): release the lock for other objects to have chance to execute. sleep(): keep lock for at least t times if timeout specified or somebody interrupt.
    – legend
    Aug 25, 2018 at 2:10
  • 3
    @legend: what you read is very misleadingly worded, to the point that I would call it wrong. sleep() has nothing to do with locking, but because of that if a thread has locks on some object, it will keep those while sleeping (which is typically very bad). The purpose of sleep() is to do nothing for a fixed time. Aug 25, 2018 at 15:38
  • 2
    @legend: The purpose of wait() is not really to wait for a specific time, but for some condition to be fulfilled and then get notified via a call to notify(). And while waiting for that condition, the thread releases to lock for the specific object that wait() is called on so that other threads can work towards fulfilling the condition. But here as well, if the thread holds other locks, they are not released - and that is how deadlocks can happen. Aug 25, 2018 at 15:41
33

Sleep() causes the currently executing thread to sleep (temporarily cease execution).

Yield() causes the currently executing thread object to temporarily pause and allow other threads to execute.

enter image description here

5
  • 19
    There are mistake in this flow: from Sleeping state to Ready-to-run we use interrupt() rather than notify() and notifyAll()
    – NguyenDat
    May 19, 2012 at 7:05
  • nice chart,it helps me to understand the life cycle of thread more deep and clear.
    – Amit
    Oct 18, 2014 at 2:24
  • 2
    So many upvotes on this answer. but please note @NguyenDat 's comment
    – rents
    Aug 15, 2015 at 21:35
  • Link at end of answer giving "This site can’t be reached | www.bpurcell.org took too long to respond"
    – Pang
    Jan 2, 2019 at 1:17
  • why does sleeping thread needs Object.notify() or Object.notifyall() to get to ready-to-run state ? Jan 2, 2020 at 5:20
5

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).

5

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.

4

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.

1
  • 2
    Is there a significant difference between your answer and the already accepted one?
    – gravity
    Jan 24, 2019 at 20:14
1

Yield: It is a hint (not guaranteed) to the scheduler that you have done enough and that some other thread of same priority might run and use the CPU.

Thread.sleep();

Sleep: It blocks the execution of that particular thread for a given time.

TimeUnit.MILLISECONDS.sleep(1000);
0

yield(): yield method is used to pause the execution of currently running process so that other waiting thread with the same priority will get CPU to execute.Threads with lower priority will not be executed on yield. if there is no waiting thread then this thread will start its execution.

join(): join method stops currently executing thread and wait for another to complete on which in calls the join method after that it will resume its own execution.

For detailed explanation, see this link.

0

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

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

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

Both methods are used to prevent thread execution. But specifically, sleep(): purpose:if a thread don't want to perform any operation for particular amount of time then we should go for sleep().for e.x. slide show . yield(): purpose:if a thread wants to pause it's execution to give chance of execution to another waiting threads of same priority.thread which requires more execution time should call yield() in between execution.

Note:some platform may not provide proper support for yield() . because underlying system may not provide support for preemptive scheduling.moreover yield() is native method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.