Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners yield and sleep
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


hello,

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


Advertisement
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
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
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
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
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
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
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
Java thread  What is the difference between wait() and sleep method
Java thread state
Java thread state  Difference between sleep and suspend
Thread Life Cycle Example in java
to another thread we can use yield() method. Example : Thread thread = new... state when it is suspend, sleep or waiting. Example : Thread thread = new... the threads into running state, yield() method to move the control to another thread
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
Threads in Java
starts the execution of a thread  sleep( )  void... the threads on which it is invoked.  yield( )  void
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
Advertisements
 

 

 

DMCA.com