• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

difference between wait and sleep?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

can any one explain d/b sleep and wait method
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sleep, suspends the current thread for a period of time.
The expiration of that time is what will make the thread runnable.

Wait, suspends the current thread until some event, like the freeing of a lock. When someone else calls a notify it will make this thread runnable again.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wait must be called from that part of code which aquires the lock otherwise it will throw an exception.

this is not the case with sleep,you can call sleep from anywhere,it will just make the calling thread sleep for the specified time.sleep is a static method.
 
Ranch Hand
Posts: 48
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by paritosh ranjan@bitm:
[QB]wait must be called from that part of code which aquires the lock otherwise it will throw an exception.
QB]



I think notify() should be called from the piece of code which has the lock and wait() should be called from piece of code which wants to acquire the lock.
 
paritosh ranjan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes

What I wanted to say was that both wait and notify should be called from a synchronized block
 
Ranch Hand
Posts: 92
Android Eclipse IDE Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another main difference between wait and sleep is that in calling wait the current executing thread releases the ownership of the monitor while in calling Thread.sleep the current thread that sleeps does not lose ownership of any monitors (if any).
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As we know java programming language supports multi threading concept. The threads are running at the same time if you want to stop execution of a thread and give the control to another thread these two functions, are supported by java for the purpose

Wait() : This function takes parameters as milliseconds and puts the thread in wait state for the desired time of the programmer after time passes the execution starts again.

Sleep() : This function is also used for same purpose using his function by java you can put a thread in sleep state .sleep does not contains any parameters so the thread will not be automatically start execution It needs a wake up signal again which can be Notify().or other function are also provided by java.

So the main difference in Wait() and sleep() is wait takes time parameter and wait for specific time only and sleep throws a thread in sleep mode for unspecified time.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

babor hossain wrote:As we know java programming language supports multi threading concept. The threads are running at the same time if you want to stop execution of a thread and give the control to another thread these two functions, are supported by java for the purpose

Wait() : This function takes parameters as milliseconds and puts the thread in wait state for the desired time of the programmer after time passes the execution starts again.

Sleep() : This function is also used for same purpose using his function by java you can put a thread in sleep state .sleep does not contains any parameters so the thread will not be automatically start execution It needs a wake up signal again which can be Notify().or other function are also provided by java.

So the main difference in Wait() and sleep() is wait takes time parameter and wait for specific time only and sleep throws a thread in sleep mode for unspecified time.



@ babor hossain
Welcome to JavaRanch

Object class have following methods

JavaDocumentation wrote:void wait() => Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

void wait(long timeout) => Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

void wait(long timeout, int nanos) => Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

void notify() => Wakes up a single thread that is waiting on this object's monitor.

void notifyAll() => Wakes up all threads that are waiting on this object's monitor.



Thread class have following method

JavaDocumentation wrote:static void sleep(long millis) => Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

static void sleep(long millis, int nanos) => Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.




 
A timing clock, fuse wire, high explosives and a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic