Results 1 to 2 of 2
  1. #1
    DukeNukem is offline Member
    Join Date
    Mar 2009
    Location
    Mordor
    Posts
    1
    Rep Power
    0

    Post Odd Even number synchronization in threads

    Hi All,

    This is my first post and I could not find any solution to the thread concurrency problem I want to solve.

    I want to write code where one thread outputs odd number and another outputs even number is proper ascending order. For ex, 1,2,3,4,5,6,7..

    I have written the following code to solve the problem, if I print the numbers directly inside the synchronized method then the problem is solved. But how do I return the numbers in proper order, from the methods getOdd() & getEven() as a return parameter. Please help.

    Java Code:
    import static java.lang.System.out;
    
    public class TestOddEven {
    
      public static void main(String[] args) {
        
        Q q = new Q();
        Even even = new Even(q);
        Odd odd = new Odd(q);
        
      }
      
    }
    
    class Q {
      
      boolean isOdd = false;
      int i = 0;
      
      synchronized int getOdd() {
        
        while(!isOdd) {
          try {
            wait();
          } catch(InterruptedException e) {}
        }
    
        isOdd = false;
        //System.out.println("Odd = = " + i++);
        notifyAll();
        
        return i++;
      }
    
      synchronized int getEven() {
        
        while(isOdd) {
          try {
            wait();
          } catch(InterruptedException e) {}
        }
    
        isOdd = true;
        //System.out.println("Even = = " + i++);
        notifyAll();
        return i++;
      }
    
    }
    
    class Odd implements Runnable {
    
      Q q;
      Thread t;
      
      Odd(Q q) {
        this.q = q;
        t = new Thread(this," --Odd-- ");
        t.start();
      }
      
      public void run() {
        for(int i=0;i<10;i++)
          System.out.println("Odd === "+q.getOdd());
    
        System.out.println("-- End thread Odd --");
      }
      
    }
    
    class Even implements Runnable {
    
      Q q;
      Thread t;
      
      Even(Q q) {
        this.q = q;
        t = new Thread(this," --Even-- ");
        t.start();
      }
      
      public void run() {
        for(int i=0;i<10;i++)
          System.out.println("Even ==="+q.getEven());
        
        System.out.println("-- End thread Even --");
      }
      
    }

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Sounds like a homework problem, so I don't want to give too much...

    You have a lot of code to do a simple task. Basically, you have two threads that do essentially the same task, which is to start at a certain value, print it, and then add 2. The threads should wait on each other.

    You need one class that accepts three parms: a start point and an object to wait on, and an object to notify. Start the threads, each waiting on its own object. Have the main notify on the odd thread object. When each thread loops, it should notify the other thread's object. Throw in a Thread.wait(15) before notifying, to avoid a race condition.

Similar Threads

  1. Replies: 0
    Last Post: 03-03-2009, 11:45 AM
  2. Animation Synchronization
    By dreadrocksean in forum Advanced Java
    Replies: 5
    Last Post: 08-08-2008, 03:56 AM
  3. synchronization question
    By oguz in forum Threads and Synchronization
    Replies: 2
    Last Post: 07-22-2008, 09:56 AM
  4. Synchronization Doesn't seem to work
    By sherinpearl in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 07:30 PM
  5. Synchronization problems
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 02:17 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
  •