1

As my understanding, if I am thread then:

  • When I call notify()/notifyAll() it means means I am sending a signal to other thread which are waiting on this object monitor. Am I right?

  • But What the wait() means?

    Am I sending signal to other thread to wait so that I can complete my work on it?
    Or
    I am declaring that I will wait, You do your work and tell me when you are done.?

I have a little concept about threads

Saif
  • 6,804
  • 8
  • 40
  • 61
  • 2
    You are waiting for some to notify you of something... – MadProgrammer Sep 25 '14 at 06:06
  • 1
    Take some time to read through [Lesson: Concurrency](http://docs.oracle.com/javase/tutorial/essential/concurrency/) and don't be afriad to try some test... – MadProgrammer Sep 25 '14 at 06:08
  • 1
    The latter. But this can be read in any documentation about this topic, including the [API doc about `wait()`](http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) itself. – Seelenvirtuose Sep 25 '14 at 06:09
  • the thread in which you declare wait() will wait. It will resume its execution when you notify him. – Mohit Sep 25 '14 at 06:13
  • 2
    i think it a good question - because this is an very very old method and the expression `wait` is not so clear!! `Thread.waitForNotificaion()` would be surely a better method name – Martin Frank Sep 25 '14 at 06:20

2 Answers2

3

No, it means you want to wait for a notification.

As per the Java docs (my emphasis):

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Then I have a little confusion. Suppose some one call notify on an object and I am not waiting on that specific object monitor but i am working with that object. Is it a valid situation?if yes then will it affect by behavior? – Saif Sep 25 '14 at 06:16
  • if you want i can update my question with this part. – Saif Sep 25 '14 at 06:16
  • 1
    Both threads need to use the same monitor object for this to work. If someone calls `notify` on an unrelated object, you will continue to wait. – Thilo Sep 25 '14 at 06:18
  • i got this part thanks. But i am just wondering as all other newbie if i am not waiting some object but using it as a property or some thing and some other thread called notify on that object? is that type of situation can happen or its not possible logically? if yes then will it affect me .@Thilo – Saif Sep 25 '14 at 06:27
  • 1
    @Saif, you need to look at the notify docs - they state "If any threads are waiting on this object, one of them is chosen to be awakened." Hence, if no threads are waiting, none can be woken up. That's why waiters should check their condition before trying to wait, in case a notify was given with no-one waiting. – paxdiablo Sep 25 '14 at 06:37
  • @Saif, `o.notify()` does not do anything at all if there are no other threads waiting in an `o.wait()` call. The object, o, does not remember that it was notified if that's what you're asking. – Solomon Slow Sep 25 '14 at 12:49
  • Exactly what i wanted to be clear of. Thanks @jameslarge – Saif Sep 26 '14 at 16:33
1

It means "I am declaring that I will wait, You do your work and tell me when you are done."

Document Says,

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

See this link

Amit
  • 13,134
  • 17
  • 77
  • 148