Forum Stats

  • 3,826,761 Users
  • 2,260,705 Discussions
  • 7,897,072 Comments

Discussions

To understand the synchronized and notifyAll().

Scottwls
Scottwls Member Posts: 10
edited Jun 23, 2017 2:05AM in Java Programming

To understand the synchronized and notifyAll().

For the following snippet,

public void addLong(Long l) {
        synchronized(this) {
            this.longs.add(l);
            notifyAll();
        }
    }
}

If I understand it correctly, the notifyAll will wake up the waiting Threads, when the current process is complete and the lock of the monitor is released.

Now, what if I code it without the notifyAll() statement, then "who" will notify the waiting Threads?  Will it be the JVM?

public void addLong(Long l) {
        synchronized(this) {
            this.longs.add(l);
        }
    }

}

Thanks.

Scott

rpc1

Answers

  • Unknown
    edited Apr 6, 2017 5:11PM
    To understand the synchronized and notifyAll().

    The way to 'understand' something is to RTFM - the documentation covers ALL of the basic functionality and would have answered your question

    https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#notifyAll()

    If I understand it correctly, the notifyAll will wake up the waiting Threads, when the current process is complete and the lock of the monitor is released.

    No - you do NOT understand it. Please read that doc about notifyAll.

    Now, what if I code it without the notifyAll() statement, then "who" will notify the waiting Threads? Will it be the JVM?

    The doc explains that.

    If there is something about what that doc says you don't understand please quote the doc and ask.

  • Scottwls
    Scottwls Member Posts: 10
    edited Apr 7, 2017 11:59AM

    I am still confused that for

    public void addLong(Long l) {
            synchronized(this) {
                this.longs.add(l);
            }
        }

    }

    There is no notify (or notifyAll) call in side the synchronised block, after the execution of the block, how will other (waiting) threads be notified?

  • Unknown
    edited Apr 7, 2017 12:35PM
    There is no notify (or notifyAll) call in side the synchronised block, after the execution of the block, how will other (waiting) threads be notified?

    Did you read what I said before?

    The doc explains that.If there is something about what that doc says you don't understand please quote the doc and ask.

    We can't read the doc for you. You have to read it yourself.

    I gave you the link to EXACTLY the place that the doc answers BOTH of your questions - one after the other.

    I just looked at the same link again and the second part has the answer you, again, just ask about.

    READ THE SECTIONS AT THAT LINK.

    Then if you have a question about what the doc says there ask. It makes no sense for us to post copies of the doc when you have the link and can just read it.

    rpc1
  • 599cdd08-d39a-4ce4-b793-97235f0eac8d
    edited Jun 23, 2017 2:05AM

    Without a notifyAll() the thread will hang.,

This discussion has been closed.