Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concept behind putting wait(),notify() methods in Object class [duplicate]

I am just having hard time to understand concept behind putting wait() in Object class. For this questions sake consider if wait() and notifyAll() are in Thread class.

class Reader extends Thread {     Calculator c;     public Reader(Calculator calc) {         c = calc;     }      public void run() {         synchronized(c) {                              //line 9         try {             System.out.println("Waiting for calculation...");             c.wait();         } catch (InterruptedException e) {}             System.out.println("Total is: " + c.total);         }     }      public static void main(String [] args) {         Calculator calculator = new Calculator();         new Reader(calculator).start();         new Reader(calculator).start();         new Reader(calculator).start();         calculator.start();     } }  class Calculator extends Thread {     int total;     public void run() {         synchronized(this) {                     //Line 31             for(int i=0;i<100;i++) {                 total += i;             }              notifyAll();         }     }  } 

My Question is that what difference it could have made? In line 9 we are acquiring lock on object c and then performing wait which satisfy the condition for wait that we need to acquire lock on the object before we use wait and so is case for notifyAll we have acquired lock on object of Calculator at line 31.

like image 306
Sunny Avatar asked Jul 24 '13 16:07

Sunny


People also ask

Why wait notify and notify all methods under object class?

If wait() and notify() were on the Thread instead then each thread would have to know the status of every other thread and there is no way to know thread1 that thread2 was waiting for any resource to access. Hence, notify, wait, notifyAll methods are defined in object class in Java.

Why wait () and notify () are in object class but not in thread class?

Shared objects allow threads to communicate by calling wait() , notify() And notifyAll() Methods, so these methods are in the object class. That's all about why wait(), notify() And notifyAll() methods Are in Object Class And Not in Thread Class.

What is the purpose of the wait () notify () and notify all () methods?

The wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() methods for that object. The notify() method wakes up a single thread that is waiting on that object's monitor. The notifyAll() method wakes up all threads that are waiting on that object's monitor.

Why wait () notify () and notifyAll () methods have to be called from a synchronized method or block?

Calling notify() or notifyAll() methods issues a notification to a single or multiple threads that a condition has changed and once the notification thread leaves the synchronized block, all the threads which are waiting for fight for object lock on which they are waiting and lucky thread returns from wait() method ...


1 Answers

I am just having hard time to understand concept behind putting wait() in object class For this questions sake consider as if wait() and notifyAll() are in thread class

In the Java language, you wait() on a particular instance of an Object – a monitor assigned to that object to be precise. If you want to send a signal to one thread that is waiting on that specific object instance then you call notify() on that object. If you want to send a signal to all threads that are waiting on that object instance, you use notifyAll() on that object.

If wait() and notify() were on the Thread instead then each thread would have to know the status of every other thread. How would thread1 know that thread2 was waiting for access to a particular resource? If thread1 needed to call thread2.notify() it would have to somehow find out that thread2 was waiting. There would need to be some mechanism for threads to register the resources or actions that they need so others could signal them when stuff was ready or available.

In Java, the object itself is the entity that is shared between threads which allows them to communicate with each other. The threads have no specific knowledge of each other and they can run asynchronously. They run and they lock, wait, and notify on the object that they want to get access to. They have no knowledge of other threads and don't need to know their status. They don't need to know that it is thread2 which is waiting for the resource – they just notify on the resource and whomever it is that is waiting (if anyone) will be notified.

In Java, we then use objects as synchronization, mutex, and communication points between threads. We synchronize on an object to get mutex access to an important code block and to synchronize memory. We wait on an object if we are waiting for some condition to change – some resource to become available. We notify on an object if we want to awaken sleeping threads.

// locks should be final objects so the object instance we are synchronizing on, // never changes private final Object lock = new Object(); ... // ensure that the thread has a mutex lock on some key code synchronized (lock) {     ...     // i need to wait for other threads to finish with some resource     // this releases the lock and waits on the associated monitor     lock.wait();     ...     // i need to signal another thread that some state has changed and they can     // awake and continue to run     lock.notify(); } 

There can be any number of lock objects in your program – each locking a particular resource or code segment. You might have 100 lock objects and only 4 threads. As the threads run the various parts of the program, they get exclusive access to one of the lock objects. Again, they don't have to know the running status of the other threads.

This allows you to scale up or down the number of threads running in your software as much as you want. You find that the 4 threads is blocking too much on outside resources, then you can increase the number. Pushing your battered server too hard then reduce the number of running threads. The lock objects ensure mutex and communication between the threads independent of how many threads are running.

like image 164
Gray Avatar answered Sep 20 '22 22:09

Gray