Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Creating more than one consumer/producer thread based on input number

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating more than one consumer/producer thread based on input number

    I am try to create more than one thread of producer and consumer based on the number from the input screen. I used this and all I got was the producer 1 set 1 and no consumer. Here is the code section:
    public void Consumer(int numberofconsumers, int sleep){
    SynBuffer buffers = new SynBuffer();
    for (int i = 0; i <= numberofconsumers; i++){
    new Thread(new Consumer(buffers,i)).start();
    }
    }
    //This constructor creates any number of consumer threads entered into the GUI screen
    public void Producer(int numberofproducers, int sleep){
    SynBuffer buffer = new SynBuffer();
    for(int i = 0; i <= numberofproducers; i++){
    new Thread(new Producer(buffer,i)).start();
    }

    sleep time for them is supposed to be taken in from the input as well. please help


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Creating more than one consumer/producer thread based on input number

    You call one a constructor, but I don't think that's what you mean. Instead, they're methods that call constructors. As methods, their names should not be capitalized.

    Since the methods are essentially identical and one produces the desired result and the other doesn't, then the problem must be in either the previous logic that calls the methods (one is called, the other isn't) or in the subsequent logic that uses the results, the producer thread is being seen but the consumer isn't. Both methods produce one object even if the desired number of objects is zero. I think that is a logic error, but that's not the source of your error. It is an indication that the problem you're trying to solve is outside the method.

    Add some print statements to ensure the methods are being called and that the desired constructors are then producing the desired objects. Also ensure that you're not getting runtime errors that you're not seeing.

Similar Threads

  1. Permanently change variable values based on user input.
    By joseph.clevenger7 in forum Java Theory & Questions
    Replies: 3
    Last Post: April 26th, 2013, 07:32 PM
  2. Print a letter of the alphabet based on a number that is input
    By justlearning in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 20th, 2013, 01:36 PM
  3. Radio button hide based on input..please help guys
    By ravi9999 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: April 7th, 2012, 06:24 AM
  4. Parsing input based on grammar rules
    By Winterfresh in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 11th, 2010, 07:54 PM
  5. Producer/Consumer Help
    By bananasplitkids in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 7th, 2010, 11:05 AM

Tags for this Thread