• Post Reply Bookmark Topic Watch Topic
  • New Topic

Behavior of threads in java  RSS feed

 
ajay mittal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am creating an object of a class B in main method of class A and invoking it's display(), which internally invoke process() of class C.

In class C I have to sleep the main thread for some interval say for eg: 40ms.

But when i use this keyword in process() of class C i donot have access to sleep() method but i have access to wait() as it is inherited by class A from Object class.

So i used Thread.sleep(40) in propcess() of class C.

I want to ask that WHY WAIT() is PLACED IN OBJECT CLASS BUT NOT SLEEP().

Ajay Mittal
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling X.wait() means the current thread is releasing X's lock and waiting until it can re-acquire it. Every object has an associated lock. That's how synchronization works.

Sleep, on the other hand, just says to pause the current thread for some amount of time.
 
ajay mittal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Calling X.wait() means the current thread is releasing X's lock and waiting until it can re-acquire it. Every object has an associated lock. That's how synchronization works.

Sleep, on the other hand, just says to pause the current thread for some amount of time.



Hi,
Yes i do know that but the question is why they did so, why they put the wait() in the Object class and not in the Thread class.

Ajay Mittal
 
Devaka Cooray
Marshal
Posts: 5650
765
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ajay mittal wrote:I want to ask that WHY WAIT() is PLACED IN OBJECT CLASS BUT NOT SLEEP().

Please KeepItDown.
If the wait() method exists only in the Thread class, you will never be able to wait for the lock of any given object, expect of Thread inheritors. Having the wait() method implemented in the Object class allows the usage what Jeff mentioned. Thread.sleep(-) method has nothing do with objects at all - not even with Thread objects - it's a static method that simply pauses the running thread for a given duration. Note that wait() and sleep(-) methods have inherently different usages.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ajay mittal wrote:the question is why they did so, why they put the wait() in the Object class and not in the Thread class.

Please SearchFirst. That question has been asked and answered several times before.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!