Forums Register Login

producer consumer thread with implementation of concurrent linked queue

+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
thank you for your input steve.

while executing the program, it gives null point exception error in the consumer side.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
thank you so much steve.
very very sorry for the delay.

your inputs were very helpful in designing the program.
+Pie Number of slices to send: Send
you are a genius steve..
the program worked perfectly with the use of BlockingQueue.
thank you so much.
+Pie Number of slices to send: Send
:-) 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.
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4948 times.
Similar Threads
join() method in Thread doesn't work as expected
Problem with Thread Interaction
Producer Consumer Problem
Multithreaded Example... Where to put WAIT() , NOTIFY() advise please
Question on Semaphores
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:26:34.