NotifyAll() Example (Java in General Forum at JavaRanch)

download NotifyAll() Example (Java in General Forum at JavaRanch)

of 4

Transcript of NotifyAll() Example (Java in General Forum at JavaRanch)

  • 7/27/2019 NotifyAll() Example (Java in General Forum at JavaRanch)

    1/4

    7/14/13 notifyAll() example (Java in General forum at JavaRanch)

    www.coderanch.com/t/524056/java/java/notifyAll

    posted 1/19/2011 8:38:51 AM

    File APIs for Java Developers

    Manipulate DOC, XLS, PPT, PDF and many others

    from your application.

    http://aspose.com/file-tools

    A friendly

    place for

    programming

    greenhorns!

    Big Moose Saloon Search | Java FAQ | Recent Topics

    Register / Login

    JavaRanch Java Forums Java Java in General

    Author notifyAll() example

    Swerrgy SmithRanch Hand

    Joined: Mar 26,2010Posts: 52

    Dear all,

    In the book of K&B, there is an example about how to use notifyAll() for thread

    communication. However, in the class Calculator below, if I remove the

    notifyAll(), the program stills works well. Can anybody tell me why?

    view pla in c opy to clipboard print ?

    Note: T ext content in the code block s is automatically word-wrapped

    01. publicclass Reader extends Thread {

    02. Calculator c;

    03.

    04. public Reader(Calculator calc) {

    05. c = calc;

    06. }

    07.

    08. publicvoid run() {

    09. synchronized(c) {

    10. try {

    11. System.out.println("Waiting for calculation

    ...");

    12. c.wait();

    http://www.javaranch.com/http://www.coderanch.com/forumshttp://www.coderanch.com/forumshttp://www.coderanch.com/forums/c/1/javahttp://www.coderanch.com/forums/c/1/javahttp://www.coderanch.com/forums/c/1/javahttp://www.coderanch.com/forums/f-1/javahttp://www.coderanch.com/forums/f-1/javahttp://www.coderanch.com/forums/f-1/javahttp://www.coderanch.com/t/524056/java/java/notifyAll#http://www.coderanch.com/t/524056/java/java/notifyAll#http://www.coderanch.com/t/524056/java/java/notifyAll#http://www.coderanch.com/t/524056/java/java/notifyAll#http://www.coderanch.com/forums/user/profile/224866http://www.coderanch.com/forums/f-1/javahttp://www.coderanch.com/forums/c/1/javahttp://www.coderanch.com/forumshttp://www.javaranch.com/http://www.coderanch.com/forums/user/loginhttp://www.coderanch.com/forums/recentTopics/listhttp://www.coderanch.com/how-to/java/Java-FAQhttp://www.coderanch.com/forums/search/filters/1http://www.coderanch.com/forums/banner/redirect/543
  • 7/27/2019 NotifyAll() Example (Java in General Forum at JavaRanch)

    2/4

  • 7/27/2019 NotifyAll() Example (Java in General Forum at JavaRanch)

    3/4

    7/14/13 notifyAll() example (Java in General forum at JavaRanch)

    www.coderanch.com/t/524056/java/java/notifyAll

    posted 1/20/2011 8:37:34 AM

    Joined: Dec 27,2010Posts: 35

    and notifyAll()? I understood serialization was not longer on the exam and I am

    familiar with the updated objectives. Is there anything new on the exam that is

    not covered in K&B SCJP 6? Sorry if this is out of scope of the topic. I have been

    studying threads a lot for the exam...regardless I know it will be helpful in the

    real world but my focus is to pass the exam.

    OCPJP for Java 6 on 01/2011

    Sunny JainRanch Hand

    Joined: Jul 23,2007Posts: 433

    I like...

    This is actually a very simple example.

    3 threads are running all on same instance, which means - synchronized(this)

    means same for all threads. Lets say we have T-1, T-2,T-3 as threads and C-1 is

    calculator instance. Let assume that -

    > T-2 got the lock over C-1, this leads to T-1 and T-3 both on waiting stage.

    > T-2 finishes the calculation and released the lock over C-1. Now you can do

    either of two things-1) Notify all the other threads waiting for C-1, by calling C-1.notifyAll( ) or

    this.notifyAll( ). order of T-1 and T-3 will be random. could be either T-1 then T-3

    or vice versa.

    2) Leave it as it is, in this case JVM will schedule the running of threads by its

    own. order of T-1 and T-3 will be random. could be either T-1 then T-3 or vice

    versa.

    Thanks,

    Thanks and Regards,SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java,java.util.concurrent tutorial

    Granny's Programming Pearls

    "inside of every large program is a

    small program struggling to get

    out"

    JavaRanch.com/granny.jsp

    subject: notifyAll() example

    Similar Threads

    doubt in thread: notifyAll()

    Question on Thread example in Kathy Sierra 1.4 book

    http://www.coderanch.com/t/240530/java-programmer-SCJP/certification/Thread-Kathy-Sierra-bookhttp://www.coderanch.com/t/441787/java-programmer-SCJP/certification/thread-notifyAllhttp://www.coderanch.com/forums/banner/redirect/494http://jovialjava.blogspot.com/search/label/Java.util.Concurrenthttp://jovialjava.blogspot.com/http://www.coderanch.com/how-to/java/BumperStickershttp://www.coderanch.com/forums/user/profile/152636
  • 7/27/2019 NotifyAll() Example (Java in General Forum at JavaRanch)

    4/4

    7/14/13 notifyAll() example (Java in General forum at JavaRanch)

    www.coderanch.com/t/524056/java/java/notifyAll

    Code using notifyAll() runs indefinitely

    notify() vs notifyAll() in this code

    Doubt w ith notify/ notifyAll method

    All times above are in your local time zone & format.The current ranch time (not your local time) is Jul 14, 2013 08:36:21.

    Contact Us | Powered by JForum | Copyright 1998-2013 Paul Wheaton

    http://www.javaranch.com/paul-wheaton.jsphttp://www.jforum.net/http://www.coderanch.com/how-to/code/ContactUshttp://www.coderanch.com/t/268669/java-programmer-SCJP/certification/notify-notifyAll-methodhttp://www.coderanch.com/t/461261/java-programmer-SCJP/certification/notify-notifyAll-codehttp://www.coderanch.com/t/503519/java-programmer-SCJP/certification/Code-notifyAll-runs-indefinitely