-1

I'm new to thread programming and I have a confusion like below.Let's take the following code block.

synchronized(obj)
{
   //do operations
   //obj.notify();
   //post operations
   // last statement 
}

Now until the "last statement" executes, the monitor for obj will not be released even after calling notify(). So is it worth calling notify() here?. Because anyway when the synchronized block exits, isn't it equal to calling notify().

Juliyanage Silva
  • 2,529
  • 1
  • 21
  • 33
  • Are you asking about `notify` in general (which happens to be the counterpart of `wait`, not synchronized blocks), or about calling it in the middle of such block? – tevemadar Oct 17 '18 at 13:18
  • 3
    My advice would be to forget about `notify()` forever. At this point, it is a low-level tool only used by experts; if you are looking to build state-dependent classes, you should build atop classes in `java.util.concurrent` like `Semaphore`, and if you don't need state dependence, you surely don't need `notify()`. – Brian Goetz Oct 17 '18 at 13:20
  • 1
    @GhostCat, I think this question is more of a duplicate for this https://stackoverflow.com/questions/15886972/automatic-notify-notifyall-on-leaving-a-synchronized-block then for one you've cited – Ivan Oct 17 '18 at 13:24
  • @tevemadar, Yeah what additional advantage it brings calling notify on objects when the code block is already being synchronized. – Juliyanage Silva Oct 17 '18 at 13:28
  • 1
    Use `wait()/notify()` to _sequence_ operations that are performed by more than one thread (e.g., If you want to make sure that a "consumer" thread does not try to take something out of a container until _after_ a "producer" has put it in to the container.) But, if all you care about is that A and B don't happen at the same time--if you don't care which happens first--then all you need for that is `synchronized`. – Solomon Slow Oct 17 '18 at 13:39

3 Answers3

2

No, when you exit synchronized block neither notify() nor notifyAll() is called and all other threads that were waiting on the same lock calling wait() will not be waken up.

Here are some cons regarding automated call to notifyAll() Automatic notify()/notifyAll() on leaving a synchronized block

Ivan
  • 8,508
  • 2
  • 19
  • 30
1

When the synchronized block exits, notify if NOT called. It only lets an eventual other thread, that was trying to enter the synchronized block, to proceeed.
notify wakes a single thread that was suspended by a call to the wait method.

Robert Kock
  • 5,795
  • 1
  • 12
  • 20
1

The synchronized block will ensure that only one thread can be in that critical section at any one time. Calling notify() on an object will wake up a single thread that is waiting on this object's monitor, i.e. obj.wait().

You don't need to use wait(), notify() or notifyAll() in most cases, including the example above.

I would recommend that you also have a look at the Executor package within Java that handles much of the complexity of you. It's very easy cause all kinds of problems when it comes to threading.

Jaco Van Niekerk
  • 4,180
  • 2
  • 21
  • 48