4 Replies Latest reply: Dec 23, 2006 11:33 AM by 796440 RSS

    concurrent programming

    807607
      HI
      i have a producer produce integer and put it in a queue, and i need to have 4 consumer get number from the queue.
      how can i synchronize the consumers?
      thank you
        • 1. Re: concurrent programming
          JosAH
          You shouldn't just synchronize the consumers, the producer(s) should
          be sync'ed as well. The best object to synchronize on is the shared pool
          (queue) of produced objects. Producers should just wait while the
          consumers are busy with that queue and consumers should wait until
          there is something present in that queue they can consume.

          kind regards,

          Jos
          • 2. Re: concurrent programming
            796440
            Read this:

            http://java.sun.com/docs/books/tutorial/essential/concurrency/


            Then google for java producer consumer example.
            • 3. Re: concurrent programming
              807607
              Hi guys
              i want to have one producer and 2 consumer .. when this code run each consumer thread are not syncronized ( ex produce = 0, 1, 2, 3, ..... int my case both consumer consume 0 , 1, 2 and so on !!!!!
              The consumer class
              class Consumer extends Thread{
                   protected SyncBoundedQueue queue;
                   protected int n;
                   protected Timer time;
              
                   public Consumer(SyncBoundedQueue queue,int n ){
                        this.queue = queue;
                        this.n = n;
                        this.time = time;
                   }
                   public void run(){
                        for(int i=0; i<n;i++){
              
                             try{
                             sleep((int)(Math.random()*1000));
                             Object obj = queue.get();
              
                             if(obj != null)
                             System.out.println("\tConsume "+ i );
                             
                             
              
                             
                             }
                             catch (InterruptedException e)
                             {
                                  e.printStackTrace();
                             }
                             
                        }
              
                   }
              }
              the get method in SyncBoundedQueue
              synchronized public Object get(){
                        try
                        {
                             while (isEmpty()){
                                  wait();
                        }
                        }catch(InterruptedException ex){ex.printStackTrace();}
                        Object result = null;
              
                          if(!isEmpty())
                          {
                               result = rep.get(0);
                               rep.remove(0);
              
                               
                          }
                          
                        notify();
                        return result;
                   }
              the main method
              public class Thread2 {
                   static SyncBoundedQueue  queue = new SyncBoundedQueue();
                   
                   public static void main (String args[]){
                        
              
                         Producer prod =  new Producer(queue,300);
                         Consumer con= new Consumer(queue,300);
                         Consumer con2 = new Consumer(queue,300);
              
              
                       
                      prod.start();
                      con.start();
                      con2.start();
              
              
                        
                   }
              }
              • 4. Re: concurrent programming
                796440
                It's not clear what your problem is. Can you explain it more clearly?