yield and sleep

yield and sleep

hello,

What is the difference between yield() and sleep()?

View Answers

April 23, 2013 at 5:39 PM

Main difference between wait and sleep is that wait() method release the acquired monitor when thread is waiting while Thread.sleep() method keeps the lock or monitor even if thread is waiting. Also wait method in java should be called from synchronized method or block while there is no such requirement for sleep() method. Another difference is Thread.sleep() method is a static method and applies on current thread, while wait() is an instance specific method and only got wake up if some other thread calls notify method on same object. also in case of sleep, sleeping thread immediately goes to Runnable state after waking up while in case of wait, waiting thread first acquires the lock and then goes into Runnable state. So based upon your need if you require a specified second of pause use sleep() method or if you want to implement inter-thread communication use wait method.

here is list of difference between wait and sleep in Java :

1) wait is called from synchronized context only while sleep can be called without synchronized block. see Why wait and notify needs to call from synchronized method for more detail.

2) wait is called on Object while sleep is called on Thread. see Why wait and notify are defined in object class instead of Thread.

3) waiting thread can be awake by calling notify and notifyAll while sleeping thread can not be awaken by calling notify method.

4) wait is normally done on condition, Thread wait until a condition is true while sleep is just to put your thread on sleep.

5) wait release lock on object while waiting while sleep doesnâ??t release lock while waiting.

Difference between yield and sleep in java

Major difference between yield and sleep in Java is that yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same 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. Yield method doesnâ??t guarantee that current thread will pause or stop but it guarantee that CPU will be relinquish by current Thread as a result of call to Thread.yield() method in java.

Sleep method in Java has two variants one which takes millisecond as sleeping time while other which takes both mill and nano second for sleeping duration.

sleep(long millis) or sleep(long millis,int nanos)

Cause the currently executing thread to sleep for the specified number of milliseconds plus the specified number of nanoseconds.









Related Tutorials/Questions & Answers:
yield and sleep
yield and sleep  hello, What is the difference between yield() and sleep
__sleep and __wakeup
__sleep and __wakeup  What?s the special meaning of _sleep and _wakeup
Advertisements
ModuleNotFoundError: No module named 'yield'
ModuleNotFoundError: No module named 'yield'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'yield' How to remove the ModuleNotFoundError: No module named 'yield'
example of sleep and wait method
example of sleep and wait method  write a program to use the sleep and wait method of thread class
ModuleNotFoundError: No module named 'sleep'
ModuleNotFoundError: No module named 'sleep'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'sleep' How to remove the ModuleNotFoundError: No module named 'sleep'
ModuleNotFoundError: No module named 'pytest-yield'
ModuleNotFoundError: No module named 'pytest-yield'  Hi, My Python... 'pytest-yield' How to remove the ModuleNotFoundError: No module named 'pytest-yield' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'sre-yield'
ModuleNotFoundError: No module named 'sre-yield'  Hi, My Python... 'sre-yield' How to remove the ModuleNotFoundError: No module named 'sre-yield' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'yield-from'
ModuleNotFoundError: No module named 'yield-from'  Hi, My Python... 'yield-from' How to remove the ModuleNotFoundError: No module named 'yield-from' error? Thanks   Hi, In your python environment you
java sleep in main method
java sleep in main method  Hi, How to write Java program for sleeping in the main method? I want Java program to have sleep in main method. Try to share me the code examples. Thanks
java sleep in main method
java sleep in main method  Hi, How to write Java program for sleeping in the main method? I want Java program to have sleep in main method. Try to share me the code examples. Thanks
What is the special meaning of __sleep and __wakeup?
What is the special meaning of __sleep and __wakeup?  What?s the special meaning of _sleep and _wakeup
Java Thread : yield() method
Java Thread : yield() method In this section we are going to describe yield() method with example in java thread. yield() Method: When you want to stop current thread and switch the CPU availability to another thread, call yield
ModuleNotFoundError: No module named 'tornado-redis-yield'
ModuleNotFoundError: No module named 'tornado-redis-yield'  Hi, My... named 'tornado-redis-yield' How to remove the ModuleNotFoundError: No module named 'tornado-redis-yield' error? Thanks   Hi
ModuleNotFoundError: No module named 'yield-curve-dynamics'
ModuleNotFoundError: No module named 'yield-curve-dynamics'  Hi...: No module named 'yield-curve-dynamics' How to remove the ModuleNotFoundError: No module named 'yield-curve-dynamics' error? Thanks   Hi
ModuleNotFoundError: No module named 'yield-curve-dynamics'
ModuleNotFoundError: No module named 'yield-curve-dynamics'  Hi...: No module named 'yield-curve-dynamics' How to remove the ModuleNotFoundError: No module named 'yield-curve-dynamics' error? Thanks   Hi
ModuleNotFoundError: No module named 'server-sleep'
ModuleNotFoundError: No module named 'server-sleep'  Hi, My Python... 'server-sleep' How to remove the ModuleNotFoundError: No module named 'server-sleep' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'sleep-after'
ModuleNotFoundError: No module named 'sleep-after'  Hi, My Python... 'sleep-after' How to remove the ModuleNotFoundError: No module named 'sleep-after' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'sleep-after'
ModuleNotFoundError: No module named 'sleep-after'  Hi, My Python... 'sleep-after' How to remove the ModuleNotFoundError: No module named 'sleep-after' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'sleep-after'
ModuleNotFoundError: No module named 'sleep-after'  Hi, My Python... 'sleep-after' How to remove the ModuleNotFoundError: No module named 'sleep-after' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'sleep-hotkey'
ModuleNotFoundError: No module named 'sleep-hotkey'  Hi, My Python... 'sleep-hotkey' How to remove the ModuleNotFoundError: No module named 'sleep-hotkey' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'sleep-parameters'
ModuleNotFoundError: No module named 'sleep-parameters'  Hi, My... named 'sleep-parameters' How to remove the ModuleNotFoundError: No module named 'sleep-parameters' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'flask-sleep'
ModuleNotFoundError: No module named 'flask-sleep'  Hi, My Python... 'flask-sleep' How to remove the ModuleNotFoundError: No module named 'flask-sleep' error? Thanks   Hi, In your python environment
Java Thread Yield
Java Thread yield is a static method It works on same priority threads It makes same priority thread from running to runnable state. Then other same priority thread comes to running state from runnable state Java Yield
Java Thread : sleep() method
Java Thread : sleep() method In this section we are going to describe sleep() method with example in java thread. sleep() method : Suppose you want to stop your thread for a specific time period, you can use sleep() method
sleep method in thread java program
sleep method in thread java program  How can we use sleep method... example ,we have used sleep method. we are passing some interval to the sleep... Description:- In this thread example ,we have used sleep method. we are passing
What?s the special meaning of __sleep and __wakeup?
What?s the special meaning of __sleep and __wakeup?  What?s the special meaning of _sleep and _wakeup
Java Sleep Thread
Java Thread sleep() is a static method. It sleeps the thread for the given time in milliseconds. It is used to delay the thread. It is used in Applet or GUI programming for animation Java Sleep Thread Example public class
ModuleNotFoundError: No module named 'matialvarezs_time_sleep'
ModuleNotFoundError: No module named 'matialvarezs_time_sleep'  Hi...: No module named 'matialvarezs_time_sleep' How to remove the ModuleNotFoundError: No module named 'matialvarezs_time_sleep' error? Thanks   
need a jar file to come out of sleep mode
need a jar file to come out of sleep mode  Hi I need a jar file... then phone goes to out of sleep mode. can any body help me with this subject ? I... it to any address by email . But when the phone goes to sleep mode
how to use yield(),join() methods in program.where it is usefull in my program
how to use yield(),join() methods in program.where it is usefull in my program  Hi i am using yield() in my program but the out put i can't undrestand means i am using yield() and without yield() same out put print.But experts
how to use yield(),join() methods in program.where it is usefull in my program
how to use yield(),join() methods in program.where it is usefull in my program  Hi i am using yield() in my program but the out put i can't undrestand means i am using yield() and without yield() same out put print.But experts
PHP Sleep Wakeup Method
The __sleep and __wakeup Function in PHP The __sleep function in PHP is useful... __sleep or not. If so, then the function execute that method prior to any other... any resources that the object may have. PHP Sleep and Wakeup Method Example
I want only one method goes to sleep but both method goes to sleep together
I want only one method goes to sleep but both method goes to sleep... want like a sleep() or any other method should be invoked between User's turn and Computer's turn. But when i call Sleep() before cpuPlay() method or after
I want only one method goes to sleep but both method goes to sleep together
I want only one method goes to sleep but both method goes to sleep... want like a sleep() or any other method should be invoked between User's turn and Computer's turn. But when i call Sleep() before cpuPlay() method or after
Extending Java with Yield
Extending Java with Yield          Yielder library is a library that uses Java 1.5's facility to hook user-defined class inspectors/transformers
threads
threads  plz tell me about wait(), sleep(),block,yield() with examples
Managing Anger
Managing Anger       Introduction Angry behavior at home, on the road and at the workplace is on the rise everywhere, what with increasing work pressures, inadequate sleep
SCJP Module-8 Question-4
void run() { for (int i = 0; i < 2; i++) { try { sleep(1000); } catch (InterruptedException e) { } yield(); System.out.println(sTname); } }} Which
java
java  what is work of the yield() in the Thread
SCJP Module-8 Question-8
Java Exception Thread
Thread  1)Notify( ) 1)Sleep...)Yield( ) 3)wait...().sleep(2000) is used to deactivate the thread untill the next thread started
Error - Java Beginners
i=1;i<5;i++) { yield...); if(k==1) try { sleep(1000); } catch(Exception... { public void run() { for(int i=1;i<5;i++) { yield
Java :Thread Methods
the priorty of the current thread.ADS_TO_REPLACE_3 sleep(long millis) - It puts the current executing thread to sleep for a given number of milliseconds... of your  thread. yield() - This method pauses and allow other threads
Java thread state
Java thread state  Difference between sleep and suspend
Java thread
Java thread  What is the difference between wait() and sleep method
Java Programming Question
Java Programming Question  write a program in java to show thread working with sleep and wait method   Hi Friend, Please visit the following links: Wait Method Sleep Method Thanks
java questions
java questions  what is the difference between wait() and sleep() methods in thread class
java
java  how can use sleep in java   which book learn of java language
Thread Life Cycle Example in java
into running state, yield() method to move the control to another thread, sleep() method.... To move control to another thread we can use yield() method.ADS_TO_REPLACE_2... and/or running state. A thread is in blocked state when it is suspend, sleep
programs - Java Beginners
the thread class, and also use the yield ( ), stop ( ), sleep ( ) methods. 9

Ads