• Post Reply Bookmark Topic Watch Topic
  • New Topic

why does the Object class have methods such as wait,notify,notifyall?

 
kush sharma
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the Object class have methods such as the wait, notify , notifyall?
also Can we extend the Thread class and implement the runnable interface at the same time?.Why I am confused is because the effectiveness of the Runnable class is lost as we don't have methods such as sleep, join and yield as its members. though we can call these methods using the Thread class which I assume must be static methods in the Thread class..
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kush sharma welcome to Javaranch ,
Firstly please choose a proper forum for you post read this for more information.
One of the moderators will move this to its proper place.

Why does the Object class have methods such as the wait, notify , notifyall?

I think for two reasons
1. Synchronization requires object level locks
2. Because the designers thought so

Can we extend the Thread class and implement the runnable interface at the same time?

But why would some one do that?
[ August 20, 2008: Message edited by: Amit Ghorpade ]
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kush sharma:
Can we extend the Thread class and implement the runnable interface at the same time?.


Look at the Thread API. Thread implements Runnable, so every time you extend Thread, you are implicitly implementing Runnable, if you declare it or not.

Originally posted by kush sharma:
Why I am confused is because the effectiveness of the Runnable class is lost as we don't have methods such as sleep, join and yield as its members. though we can call these methods using the Thread class which I assume must be static methods in the Thread class..


The effectiveness of a Runnable is that it is the simplest means to define an task that runs in its own thread. If you don't need to add on to the functionality of a Thread, why make your class a full-blown Thread?

In Thread, sleep() and yield() are static, and work on the current Thread. But join() is not. You need an instance of a Thread to join() to, which is usually not the current Thread (why would you need to joint() to the current Thread, you already are running in the current Thread). You don't lose this ability when using a Runnable, after all you still create a Thread to run the Runnable in, so you can hold a reference to that and join() with it when you need to:

[ August 20, 2008: Message edited by: Steve Luke ]
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Steve]: why would you need to joint() to the current Thread, you already are running in the current Thread

Actually, it's impossible to join() the current thread - because join() will wait until the thread dies, and the thread can't die as long as it's waiting. If you try to join the current thread, the method call will block forever - which is to say, until you interrupt() it or kill the JVM.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Simmons:
[Steve]: why would you need to joint() to the current Thread, you already are running in the current Thread

Actually, it's impossible to join() the current thread - because join() will wait until the thread dies, and the thread can't die as long as it's waiting. If you try to join the current thread, the method call will block forever - which is to say, until you interrupt() it or kill the JVM.


Right, it was a hypothetical question, reasoning why join() works on a specific thread instance rather than as a static method which works on the current thread like sleep() and yield().

It isn't possible, but even if it were technically possible there would be no reason to do it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!