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