• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Specific methods why in Object class ?  RSS feed

 
Monoj Roy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I have worked with wait(), notify() and notifyall() methods whenever I have worked with threads but I have not used those anytime where I am not concern in thread . Can anybody suggest Why are wait(), notify() and notifyall() methods defined in the Object class? Why not in Thread Class ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

We use some object to communicate between threads. One thread waits, the other thread notifies. When we're waiting, we have no idea what thread might notify, so we can't wait on the notifying Thread. When we're notifying we have no idea what threads might be waiting, so we can't notify on waiting Threads. So we pick some other object, any object really, and pass it around just for communication.

If you have more thread questions, scroll up to the dedicated Threads forum for all the experts.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15865
81
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

The answer to your question: These methods are in class Object because threads in Java can synchronize on any object (not just on Thread objects).
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More than that, it is usually wrong to synchronise on a Thread object, or to call wait() or notify() on a Thread object. So, it's pretty clear that these methods do not belong in the Thread class.

What's less clear is why they belong in the Object class and are hence available directly to all objects. That was a decision that the Java language designers took, I suspect partly to make it clear just how seriously Java took multi-threading.

The decision could equally have been taken to introduce some other class(es) for supporting synchronisation, notification etc. But that's not how it is, and worrying about it too much would be a mistake.
 
Monoj Roy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot to give some light on my issue .

I have still some confusion. Lets take an example and discuss the point on the example.
I am taking the following Example

Lets think there is two Thread ThA and ThB and there is another Object ObjX

ThA extends Thread{
public void run(){
ObjX x = new ObjX();
x.method1();

}

}

ThB extends Thread{
public void run(){
ObjX x = new ObjX();
x.method1();
}

}

class ObjX{

synchronize method1(){
Syatem.out.println("I am in the method1 current theread is"+Thread.currentThread())
}

}

public static void main(String args[]){
ThA a = new ThA();
ThB b = new ThB();

a.start();
b.start();

}


If we run this it will run and the thread A and Thread B will start working .As method1() is synchronize we can wait a ThA or ThB to give a good flow depending on our requirement ..

But my question is I want to use x.wait() or x.notify(); How that is possible and in what condition I can wirite x.wait() or x.notify();
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically your issue is your not synchronizing, waiting etc on an object common to both threads ...

If you want simple synchronization then make method1 static though there is no need for wait notify.

If you want wait / notify and you don't want to use static synchronization i.e. syncing on the class object then the simplest for your purposes is ...

create a new object in main, pass this object into the constructors of your two threads and you can then synchronize, wait , notify etc. on the common object.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!