Results 1 to 7 of 7
  1. #1
    dzastin is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default Multithreading-in-Java

    Hello,

    I am looking at second example in Page 9 - Multithreading in Java

    and i don't get expected output:
    Put: 0
    Get: 0
    Put: 1
    Get: 1
    Put: 2
    Get: 2
    Put: 3
    Get: 3
    Put: 4
    Get: 4

    I get
    run:
    Put: 0
    Get: 0
    Put: 1
    Get: 1
    Put: 2
    Get: 2
    Get: 2
    Get: 2
    Put: 3 (Force STOP)

    or

    run:
    Put: 0
    Get: 0
    Get: 0
    Get: 0
    Get: 0
    Get: 0
    Put: 1
    BUILD STOPPED (total time: 2 seconds)

    Maybe my neatbeans 7 beta has something to do with this?

    Java Code:
    class Queue {
       int exchangeValue;
        boolean busy = false;
        synchronized int get() {
           if (!busy)
              try {
                 wait();
              } catch (InterruptedException e) {
                   System.out.println(
                       "Get: InterruptedException");
              }
            System.out.println("Get: " + exchangeValue);
             notify();
           return exchangeValue;
        }
        synchronized void put (int exchangeValue) {
           if (busy)
              try {
                 wait();
              } catch (InterruptedException e) {
                   System.out.println(
                        "Put: InterruptedException");
              }
           this.exchangeValue = exchangeValue;
           busy = true;
           System.out.println("Put: " + exchangeValue);
           notify();
        }
    }
    class Publisher implements Runnable {
       Queue q;
       Publisher(Queue q) {
          this.q = q;
          new Thread (this, "Publisher").start();
       }
       public void run() {
          for (int i = 0; i < 5; i++){
             q.put(i);
          }
       }
    }
    class Consumer implements Runnable {
       Queue  q;
       Consumer (Queue  q) {
          this.q = q;
          new Thread (this, "Consumer").start();
       }
       public void run() {
          for (int i = 0; i < 5; i++){
             q.get();
          }
       }
    }
    class Demo {
       public static void main(String args []) {
         Queue q = new Queue ();
         new Publisher (q);
         new Consumer (q);
       }
    }

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    5

    Default

    Hi, This is code works fine. What you expected output?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    dzastin is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    i get this:
    Put: 0
    Get: 0
    Get: 0
    Get: 0
    Get: 0
    Get: 0
    Put: 1
    BUILD STOPPED (total time: 3 seconds)

    And i have to manually kill program. What IDE are you using? I will try uninstall 7 netbeans and install 6.9.1

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default

    Your put() and get() methods aren't symmetrical: the busy variable should keep track whether or not a value has been put there (true); after it is read, the value should be false; change those two methods to:

    Java Code:
        synchronized int get() {
           if (!busy)
              try {
                 wait();
              } catch (InterruptedException e) {
                   System.out.println("Get: InterruptedException");
              }
            int exchangeValue= this.exchangeValue;
            busy= false;
            System.out.println("Get: " + exchangeValue);
            notify();
            return exchangeValue;
        }
        
        synchronized void put (int exchangeValue) {
           if (busy)
              try {
                 wait();
              } catch (InterruptedException e) {
                   System.out.println("Put: InterruptedException");
              }
           this.exchangeValue = exchangeValue;
           busy = true;
           System.out.println("Put: " + exchangeValue);
           notify();
        }
    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    5

  6. #6
    dzastin is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks Josah,

    everything works correctly now. Maybe the author of this example wanted to demonstrate one deadlock too :).

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by dzastin View Post
    Thanks Josah,

    everything works correctly now. Maybe the author of this example wanted to demonstrate one deadlock too :).
    If you have copied that example verbatim it was definitely incorrect. As you know now it has nothing to do with NetBeans; it just ran your (incorrect) code. It wasn't deadlock either, that variable was never set to false so the producer assumed that it had to wait (forever).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Java Multithreading Problem
    By avirit1983 in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-03-2010, 11:18 PM
  2. Multithreading Gui
    By BUGSIE91 in forum Threads and Synchronization
    Replies: 7
    Last Post: 10-13-2010, 02:20 PM
  3. Multithreading in java
    By MuslimCoder in forum New To Java
    Replies: 5
    Last Post: 02-20-2010, 09:16 PM
  4. multithreading
    By shilpa.krishna in forum New To Java
    Replies: 2
    Last Post: 06-27-2008, 04:18 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •