Home Answers Viewqa Java-Beginners yield and sleep

 
 


samar
yield and sleep
1 Answer(s)      2 years and 8 months ago
Posted in : Java Beginners

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 Pages:
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
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
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
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
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
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
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
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
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
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... that the object may have. PHP Sleep and Wakeup Method Example: <?php
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
threads
threads  plz tell me about wait(), sleep(),block,yield() with examples
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
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
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
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 Exception Thread
; 1)Notify( ) 1)Sleep( ) 2)Notify all( ) 2)Yield...().sleep(2000) is used to deactivate the thread untill the next thread started
Java :Thread Methods
. sleep(long millis) - It puts the current executing thread to sleep... calls the run method of your  thread. yield() - This method pauses...), setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh), sleep
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
programs - Java Beginners
the thread class, and also use the yield ( ), stop ( ), sleep ( ) methods. 9
programs - Java Beginners
the thread class, and also use the yield ( ), stop ( ), sleep ( ) methods. 9
Java Interview Questions - Page 6
; Answer: When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state
Threads in Java
starts the execution of a thread  sleep( )  void... the threads on which it is invoked.  yield( )  void
Life Cycle of Threads
use the method sleep( ) to stop the running state of a thread.       static void sleep(long millisecond) throws... for applications.  sleep( )  void
Core Java Interview Question, Interview Question
between yielding and sleeping? Answer: When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method
Core Java Interview Question, Interview Question
thread in place of the yielding thread. Yield is a static method of class Thread... anything and without using the CPU. A call to sleep method requests the currently...: Does the thread method start executing as soon as the sleep time is over
Y - Java Terms
Y - Java Terms       Extending Java with Yield Yielder library is a library that uses Java 1.5's facility to hook user-defined class inspectors
Thread.sleep - Java Beginners
provided? Thanks taylordk@nationwide.com  The sleep() method of the thread is a static method. Calling sleep() causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds
help me...
help me...  there is the picture given - cat - dog - rabbit - mouse a. create a class name b. create its attributes (at least 3 attributes) : species, size, leg c. create its methods ( at least 3 methods) : walk, eat, sleep
Java - Threads in Java
in the sleep function of the thread class. Sleep function contains the sleeping time in millisecond and in this program sleep function has contained 5000 millisecond mean 5 second time. There is sleep function must caught
error rectify
); Thread.currentThread().sleep(1000); /* if(kbhit()) exit(0
tagged/marker interface in java - Java Beginners
: if(objectA instanceof ClassB) will yield true if objectA can be upcast to type ClassB
how can i calculate loan - Java Interview Questions
how can i calculate loan  negotiating a consumer loan is not always straightforward.one form of loan is the discount installment loan, which works as follows. suppose a loan has a face value of 1,000 by 0.15 to yield 225
PHP Magic Methods
() __sleep() __wakeup() __toString() __invoke() __set
Animating Images in Java Application
milliseconds. This collection of images makes the application as an animation. sleep(int time): This is the method of the Thread class which is used to sleep the program for the specified time. The time to sleep is passed through the sleep() method
Delay Example
that there are delay between the execution of program. In this example we are using a sleep.... This mean at execution time our program will wait for 4000 millisecond. The sleep... the sleep() method within try/catch block.    
Parallel Processing
amount of time.       static void sleep(long... is alive or not.    static void yield() ? By invoking... it.    If a thread goes to sleep, it holds any locks it has
Splash Screen in Java Swing
for the certain amount of time. We have used sleep() method of Thread class to create
Extending thread - Java Beginners
()); try { sleep((int)(Math.random() * 1000