Results 1 to 2 of 2
  1. #1
    kosmokh is offline Member
    Join Date
    Jun 2014
    Posts
    10
    Rep Power
    0

    Default Consumer / producer multi threading scenario

    Hi Forum,

    Just some questions regarding a Consumer producer program I am not understanding. Here are the two classes Below and my questions are :

    1. In the main method for NamedConsumer, we created an instance of the class and call it "consumer" and we start it. Then we create another instance and we give its reference to the same variable. Doesn't this in turn destroy the original reference of the first instance we created which was started? Could we have done

    NamedConsumer consumer1 = new NamedConsumer( "One", producer );
    new Thread( consumer1 ).start();
    NamedConsumer consumer2 = new NamedConsumer( "Two", producer );
    new Thread( consumer2 ).start();


    2. we called start on two difference instances of the NameConsumer class. Doesn't the "run" method need to be synchronized since two threads are hitting it?

    Producer.Java

    Java Code:
    public class Producer implements Runnable
    {
        static final int MAXQUEUE = 5;
        private List<String> messages = new ArrayList<String>();
    
        public void run() {
    		while ( true ) {
    			putMessage();
                try { 
    				Thread.sleep( 1000 ); 
    			} catch ( InterruptedException e ) { }
    		}
        }
    
        private synchronized void putMessage() 
    	{
            while ( messages.size() >= MAXQUEUE )
    			try {
    				wait();
    			} catch( InterruptedException e ) { }
    
            messages.add( new java.util.Date().toString() );
            notifyAll();
        }
    
        // called by Consumer
        public synchronized String getMessage()
    	{
            while ( messages.size() == 0 )
    			try {
    				notifyAll();
    				wait();
    			} catch( InterruptedException e ) { }
            String message = (String)messages.remove(0);
    		notify();
            return message;
        }
    }
    NamedConsumer.Java


    Java Code:
    //file: NamedConsumer.java
    
    public class NamedConsumer implements Runnable 
    {
        Producer producer;
        String name;
    
        NamedConsumer(String name, Producer producer) {
            this.producer = producer;
            this.name = name;
        }
    
        public void run() {
            while ( true ) {
                String message = producer.getMessage();
                System.out.println(name + " got message: " + message);
    			try { 
    				Thread.sleep( 2000 ); 
    			} catch ( InterruptedException e ) { }
            }
        }
    
        public static void main(String args[]) {
            Producer producer = new Producer();
            new Thread( producer ).start();
    
            NamedConsumer consumer = new NamedConsumer( "One", producer );
            new Thread( consumer ).start();
            consumer = new NamedConsumer( "Two", producer );
            new Thread( consumer ).start();
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: Consumer / producer multi threading scenario

    Doesn't this in turn destroy the original reference of the first instance we created which was started?
    Yes. It shouldn't be a problem unless that first reference is needed for something.

    Doesn't the "run" method need to be synchronized since two threads are hitting it?
    The instances of the class have their own variables and shouldn't conflict.
    If they were synchronized, that would remove the advantages of their being threads and cause the methods to be executed one after the other instead of having overlapping execution (depending on the cores of the CPU).
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Re: Producer Consumer Synchronization Problem
    By djery7 in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-12-2012, 10:21 PM
  2. Consumer/Producer with random numbers
    By devo22 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-13-2011, 07:57 PM
  3. producer/consumer problem
    By concaf in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-20-2011, 03:07 PM
  4. Producer-Consumer Problem
    By kendel in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-04-2011, 02:09 PM
  5. Producer Consumer Synchronization Problem
    By rushhour in forum Threads and Synchronization
    Replies: 3
    Last Post: 11-23-2010, 08:44 PM

Posting Permissions

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