Threads synchronization


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Threads synchronization

  1. #1
    Join Date
    Aug 2005
    Posts
    1

    Question Threads synchronization

    I've defined a Calculator thread which calculates a sum and makes the result available through the getTotal method. The Reader thread keeps a reference to the Calculator and prints the result.
    As I wanted the result to the be printed only after the calculation, Reader calls wait() before calling getTotal(). Calculator calls notify() as soon as the result is ready.
    In the main method, one Calculator object and three Reader objects are instantiated. The source code is presented below:


    Code:
     public class Calculator extends Thread {
     
         private int total;
         
         public void run() {
             for (int i = 0; i < 10; i++)
                 total += i;
             try {
                 Thread.sleep(1000);
             }
             catch (InterruptedException e) {
                 e.printStackTrace();
             }
     
             synchronized (this) {
                 notify();
             }
         }
         public int getTotal() {
             return total;
         }
     }
    Code:
    public class Reader extends Thread {
         Calculator c;
     
         public Reader(Calculator c) {
             this.c = c;
         }
     
         public void run() {
             synchronized (c) {
                 try {
                     String name = Thread.currentThread().getName();
                     System.out.println(name + " is waiting...");
                     c.wait();
                     System.out.println("(" + Thread.currentThread().getName() + ") total is: " 
                       + c.getTotal());
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
             }
     
         }
     
         public static void main(String[] args) {
             Calculator c = new Calculator();
             new Reader(c).start();
             new Reader(c).start();
             new Reader(c).start();
             c.start();
         }
     }
    Now the curious stuff: as notify() in called just once, at most one Reader thread would print the result. However, the program ended with the following output:

    Thread-1 is waiting...
    Thread-2 is waiting...
    Thread-3 is waiting...
    (Thread-1) total is: 45
    (Thread-2) total is: 45
    (Thread-3) total is: 45

    Even if I leave out the line with the notify() call, the output is the same.
    The program just worked as I expected after I modified the way the Calculator thread is started, changing c.start(); to new Thread(c).start();:

    Thread-1 is waiting...
    Thread-2 is waiting...
    Thread-3 is waiting...
    (Thread-1) total is: 45

    How is it possible ? I thought the synchronization mechanism was independent of the way threads were created. May I consider this a spurious wakeup ?

    Thanks in advance

  2. #2
    Join Date
    May 2005
    Posts
    25
    try notifyAll().

Similar Threads

  1. access violation using threads in Applet
    By martinfr79 in forum Java
    Replies: 1
    Last Post: 05-15-2005, 11:18 AM
  2. Heavy math routines and separate threads?
    By rechmbrs in forum Java
    Replies: 13
    Last Post: 03-22-2005, 03:14 PM
  3. Threads
    By Jonas in forum .NET
    Replies: 2
    Last Post: 02-16-2002, 11:51 AM
  4. Replies: 1
    Last Post: 12-10-2000, 03:59 PM
  5. Replies: 0
    Last Post: 09-26-2000, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center