4 Replies Latest reply: Dec 23, 2006 1:41 PM by 796440 RSS

    concurrent programming

      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
          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,

          • 2. Re: concurrent programming
            Read this:


            Then google for java producer consumer example.
            • 3. Re: concurrent programming
              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++){
                             Object obj = queue.get();
                             if(obj != null)
                             System.out.println("\tConsume "+ i );
                             catch (InterruptedException e)
              the get method in SyncBoundedQueue
              synchronized public Object get(){
                             while (isEmpty()){
                        }catch(InterruptedException ex){ex.printStackTrace();}
                        Object result = null;
                               result = rep.get(0);
                        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);
              • 4. Re: concurrent programming
                It's not clear what your problem is. Can you explain it more clearly?