Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

producer consumer thread with implementation of concurrent linked queue

 
Abhishek Mhptr
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all...

i have written a code for producer consumer thread by implementing concurrent linked queue.
where the producer thread passes objects into the queue for regular interval of time and consumer thread takes it out.

but the code is not working. kindly help me out in running the program with out errors.

the progrm consists of 4 classes.
======================================================

cricket class :
-------------

class Cricket
{

private int score;

public int getScore()
{
return score;
}

public void setScore(int score)
{
this.score= score;
}
======================================================

producer class:
------------------

import java.util.concurrent.ConcurrentLinkedQueue;


class Producer implements Runnable
{

Cricket cricket=new Cricket();

ConcurrentLinkedQueue concurrentLinkedQueue;

public Producer(ConcurrentLinkedQueue concurrentLinkedQueue)
{
this.concurrentLinkedQueue=concurrentLinkedQueue;
}

public void run()
{
for(int i=0;i<200;i++)
{

int randomNo=(int)(Math.random()*5);

cricket.setScore(randomNo);

concurrentLinkedQueue.add(cricket);


try{

Thread.sleep(randomno*1000);
}catch(InterruptedException e) { }

}
}
}
==================================================================

consumer class:
-------------------

import java.util.concurrent.ConcurrentLinkedQueue;

class Consumer implements Runnable
{
Cricket cricket=new Cricket();

ConcurrentLinkedQueue concurrentLinkedQueue;

public Consumer(ConcurrentLinkedQueue concurrentLinkedQueue)
{
this.concurrentLinkedQueue=concurrentLinkedQueue;
}


public void run()
{

for(int i=0;i<200;i++)
{

cricket=(Cricket)concurrentLinkedQueue.poll();
System.out.println("score :" + cricket.getScore());

}

}
}

================================================

Client class:
--------------

import java.util.concurrent.ConcurrentLinkedQueue;

public class Client
{

public static void main(String args[])
{

ConcurrentLinkedQueue concurrentLinkedQueue=new ConcurrentLinkedQueue();
Producer producer=new Producer(concurrentLinkedQueue);
Consumer consumer=new Consumer(concurrentLinkedQueue);


Thread t1=new Thread(producer);
t1.start();


Thread t2=new Thread(consumer);
t2.start();
}
}
==============================================================

please help me in fixing the bugs.

Thanks in advance..
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abhishek.

Next time you post code, please use code tags: [code]Your Code Here[/code]. You can generate them using the [Code] button at the top of the message form.

Also, you tell us that the code doesn't work, but don't tell us what is wrong. This type of question is hard to answer. Tell us what is wrong. What errors or exceptions do you get? What behavior do you get that you do not expect?
 
Abhishek Mhptr
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your input steve.

while executing the program, it gives null point exception error in the consumer side.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Mohapatro wrote:thank you for your input steve.

while executing the program, it gives null point exception error in the consumer side.


Can you Tell the Details? What line causes the error?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see the likely cause. You are using a ConcurrentLinkedQueue which is a good queue to use when working with multiple threads that both can put and pull from the Queue. Your scenario is different. You have one thread that only takes from the queue, and another one that only puts. Your Consumer uses poll and never checks if there was a value returned. So if the Consumer polls twice since the last time the Producer put a value in the list, you would get a Null value returned. You could fix this using synchronized blocks and notification between threads, or a continuous polling technique. But a better option would be to use a different Queue implementation that provides a method that will cause the Consumer to wait until a value becomes available before continuing.

Lucky for you there are several, all are implementations of the BlockingQueue. Your producer would use the put() method rather than the add(), and your consumer would use a take() method rather than poll(). This would ensure no null values, and lets the BlockingQueue handle synchronizing between threads. Read the API in the link provided for details, and for implementing classes to choose which is best for your use-case.
 
Abhishek Mhptr
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much steve.
very very sorry for the delay.

your inputs were very helpful in designing the program.
 
Abhishek Mhptr
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are a genius steve..
the program worked perfectly with the use of BlockingQueue.
thank you so much.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:-) I am glad you got it working. Thanks for the praise, but I assure you it isn't genius, it is just a bit of experience.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!