Results 1 to 5 of 5
  1. #1
    Cropcircles is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Threads synchronization with wait() & notify() problem

    Hello there,

    I could really appreciate if any of you could help me with this piece of code. I cannot figure out the error. I'm trying to fix it since hours, but I guess I am just too stucked in and I could really use some help.

    I have something like a producer - consumer structure. You cannot consume, unless it was previously produced. Therefore, I have used 2 synchronized threads in order to fullfill this requirement.

    I also have some brief code comments, in order to be more easy to understand.

    Java Code:
    import java.util.ArrayList;
    
    public class Test {
        public static void main(String[] args){
        	
            	Buffer b = new Buffer();
            	
                Producer pro = new Producer(b);
                Consumer c = new Consumer(b);
                Consumer c2 = new Consumer(b);
               
                Thread t1 = new Thread(pro);
                
                t1.start();
                c.start();
                c2.start();
        }
    }
     
    /*Producer class.I generate inside a infinite loop numbers of type double and they are added into a Buffer object (see below Buffer class), using put method, each 1 second.*/
    
    class Producer implements Runnable
    {
          private Buffer bf;
          private Thread thread;
          Producer(Buffer bf)
          {
        	  this.bf=bf;
          }
     
         public void start()
          {
                if (thread==null)
                {
                      thread = new Thread(this);
                      thread.start();
                }
          }
          
      
          public void run()
          {
                while (true)
                {
                      bf.put((double)(Math.random()));	//add in buffer.
                      System.out.println("Written in buffer.");
                      try
                      {Thread.sleep(1000);}catch(Exception e){}	//add  in buffer each 1 second.
                }
          }
    }
     
    //Consumer class. Inside a infinite loop we read elements of type Buffer.
     
    class Consumer extends Thread
    {
          private Buffer bf;
          Consumer(Buffer bf)
          {
        	  this.bf=bf;
          }
         
          public void run()
          {
                while (true)
               {
                      System.out.println("Read from buffer: "+this+" >> "+bf.get());
               }
          }
    }
     
    class Buffer
    {
         
          ArrayList content;// we use it in order to store double elements.
         
          /*The method put() - used to add elements in content.
             The method get() - used to extract elements from content.
             Those are synchronized methods - when we put a number in buffer (put()) - we can perform the method get() (which was previously on wait).
    */
    
          synchronized void put(double d)
          {
                content.add(new Double(d));
                notify();
          }
         
        
          synchronized double get()
          {
                double d=-1;
                try
                {
                      if(content.isEmpty()) 
                    	  	wait();	// the buffer is empty - get() is on hold.
                      d = (((Double)content.get(0))).doubleValue();
                      content.remove(0);	// the element was removed from the buffer.
                }catch(Exception e){e.printStackTrace();}
                return d;
          }
    }
    Your help will be highly appreciated. Thank you :)
    Last edited by Norm; 03-24-2012 at 09:03 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Threads synchronization with wait() & notify() problem

    I cannot figure out the error.
    Can you explain what the error is?
    Copy and paste here the debug print outs from when you execute the code that shows the execution flow and sequences of events.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Cropcircles is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Threads synchronization with wait() & notify() problem

    Thank you, Norm, for wanting to help me. I have already corrected my error:

    I have used ArrayList<Double> content = new ArrayList<Double>() insetad of ArrayList content = new ArrayList(); and also, I' ve replaced double d=-1; with Double d = new Double(-1); and I just made the small modifications that derive from this and everything works just fine now :)

    Thanks. Have a good day :)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Threads synchronization with wait() & notify() problem

    That was an easy one.

  5. #5
    nicephotog is offline Member
    Join Date
    Mar 2012
    Posts
    41
    Rep Power
    0

    Default Re: Threads synchronization with wait() & notify() problem

    class Consumer extends thread

    //---------------------------------- public void run is used only when Runnable Interface is implemented , run() from class Thread simply returns alike sleep(Xnsec); from a costructor in thread
    //---------------------------------- or if you say this.run() in the Consumer constructor. However, if you want to say this(); as the first line of the constructor for Consumer then this.run()
    //---------------------------------- should apply the overridden custom run() method without ambiguity.
    public void run()
    {
    while (true)
    {
    System.out.println("Read from buffer: "+this+" >> "+bf.get());
    }
    }
    }

Similar Threads

  1. Wait() and Notify()
    By SiX in forum New To Java
    Replies: 15
    Last Post: 07-28-2011, 04:29 PM
  2. wait() and notify()
    By jomypgeorge in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 08:58 AM
  3. Need help with wait and notify
    By mityay in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-06-2011, 04:24 PM
  4. Need help with wait() and notify()
    By Mkaveli in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-30-2010, 11:58 AM
  5. Wait() notify() implementation
    By georges in forum Advanced Java
    Replies: 4
    Last Post: 02-05-2010, 01:33 AM

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
  •