Question: 6. Implement all of the above for an arbitrary number of producers and consumers using Javas default LinkedList , rather than LinkedBlockingQueue . Note the
6. Implement all of the above for an arbitrary number of producers and consumers using Javas default LinkedList
[30] LinkedList
from the list. The total number of Strings added by all producers isexactly 1000.
[30] LinkedList
[30] If the LinkedList
Object notify()) the producers to let them add more Strings.
[10] All other requirements are the same as in the LinkedBlockingQueue
The currnet code I have is:
///////////////////////////////////////////////////////////////////////Producer.java////////////////////////////////////////////////////////////////////////////////////////////
package com.java.Thread.Assignment1;
import java.util.concurrent.BlockingQueue;
public class Producer implements Runnable {
private BlockingQueue
// Constructor
public Producer(BlockingQueue
this.blockingQueue = q;
}
@Override
public void run() {
int counter = 0;
for (int i = 0; i < 1000; i++) {
String randomString = createRandomString();
try {
Thread.sleep(i);
blockingQueue.put(randomString);
counter++;
if (counter == 100) {
System.out.println("Produced " + i);
counter = 0;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Producer STOPPED.");
}
public String createRandomString() {
double a = Math.random();
return Double.toString(a);
}
}
//////////////////////////////////////////////////////////End Producer.java//////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////Consumer.java///////////////////////////////////////////////////////////////////////////////////////
package com.java.Thread.Assignment1;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class Consumer implements Runnable {
private BlockingQueue
public Consumer(BlockingQueue
this.blockingQueue = q;
}
@Override
public void run() {
int counter = 0;
int reset = 0;
try {
String threadId = "Consumer" + Thread.currentThread().getId();
// String str;
// consuming messages until exit message is received and wait for some value
// from 0-10 millisecond
while (true) {
String number = blockingQueue.poll(5, TimeUnit.SECONDS);
if (number == null) {
break;
}
// str = blockingQueue.take().toString();
int sleepTime = generateRandomSleepTime(0, 10);
Thread.sleep(sleepTime);
reset++;
if (reset == 100) {
System.out.println("Consumed " + counter + "By" + threadId);
reset = 0;
}
counter++;
}
System.out.println(threadId + " Customer STOPPED and has consumed: " + counter + " strings");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private int generateRandomSleepTime(int min, int max) {
Random random = new Random();
int randomNum = random.nextInt((max - min) + 1) + min;
return randomNum;
}
}
/////////////////////////////////////////////////////////////////////////End Consumer.java//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////Driver.java//////////////////////////////////////////////////////////////////////////////////////////
package com.java.Thread.Assignment1;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Driver {
public static void main(String args[]) {
BlockingQueue
Producer producer = new Producer(blockingQueue);
Consumer consumer = new Consumer(blockingQueue);
new Thread(producer).start();// produce messages in queue
new Thread(consumer).start(); // consume messages from queue
new Thread(consumer).start();// Another Consumer
System.out.println("Service Started for Producer and Consumer");
}
}
//////////////////////////////////////////////////////End Driver.java/////////////////////////////////////////////////////////////////
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
