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, rather than LinkedBlockingQueue. Note the following in your implementation:

[30] LinkedList is not thread-safe, so you need to make sure every time there is only one thread, either a producer or a consumer, exclusively adds or removes a String to or

from the list. The total number of Strings added by all producers isexactly 1000.

[30] LinkedList has no maximum capacity, so you need to find a way to enforce the capacity of 100, so that if this capacity is reached then the producers need to wait (i.e., using the Object wait() method) before adding more Strings.

[30] If the LinkedList is empty (i.e., nothing to consume for the consumer) but the total added Strings has not reached 1000, then you need to wake up (i.e., using the

Object notify()) the producers to let them add more Strings.

[10] All other requirements are the same as in the LinkedBlockingQueue implementation. Sleep before every String consumption. Print the progress once every 100 Strings are added or consumed, and print a summary information about the exact number of Strings added and consumed by each producer or consumer. Execute your program with at least 2 producers and at least 2 consumers concurrently. Program automatically quits when all production and consumption are done.

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 blockingQueue;

// Constructor

public Producer(BlockingQueue q) {

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 blockingQueue;

public Consumer(BlockingQueue q) {

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 blockingQueue = new LinkedBlockingQueue(100);

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!