I am stuck in this problem, it works fine in my IDE but has compile errors in the online compiler where i'm submitting my code. Please help.
Write a multi threaded Java program to implement Producer and Consumer. Span two thread for producer and consumer from the main method. Using wait() and notify() methods for co-ordinating between threads are error-prone, so in this execise we will use the blocking queue which is used for message passing between multipe threads. In this problem the producer inserts the number from 0 - N into the blocking queue and the consumer listens on this queue for any information. Once the data is inserted the consumer takes the message or number and prints on the console.
Sample Input:
**5**
Sample Output :
Consume value: 0
Consume value: 1
Consume value: 2
Consume value: 3
Consume value: 4
Following is the code:
**Main class**
public class Main {
public static void main(String[] args) throws InterruptedException {
BlockingQueue sharedQ = new LinkedBlockingQueue();
System.out.println("Number of Elemets:");
Scanner sc = new Scanner(System.in);
int numberofElements = sc.nextInt();
ConsumerThread consumerThread = new ConsumerThread(sharedQ, numberofElements);
ProducerThread producerThread = new ProducerThread(sharedQ, numberofElements);
producerThread.start();
consumerThread.start();
producerThread.join();
}
}
Thread classes
public class ConsumerThread extends Thread {
private final BlockingQueue sharedQ;
private final int numberofElements;
public ConsumerThread(BlockingQueue sharedQ,int numberofElements) {
this.sharedQ=sharedQ;
this.numberofElements=numberofElements;
}
public void run() {
for(int i = 0; i < numberofElements; i++) {
try {
System.out.println("Consumed value " + sharedQ.take());
} catch(InterruptedException ie) {
System.err.println("Error :: " + ie);
}
}
}
}
public class ProducerThread extends Thread {
private final BlockingQueue sharedQ;
private final int numberofElements;
public ProducerThread(BlockingQueue sharedQ,int numberofElements) {
this.sharedQ=sharedQ;
this.numberofElements=numberofElements;
}
public void run() {
for(int i = 0; i < numberofElements; i++) {
try {
Random random = new Random();
int number = i;
// System.out.println("Producing value " + number);
sharedQ.put(number);
} catch(InterruptedException ie) {
System.err.println("Error :: " + ie);
}
}
}
}
The error is
Compilation Errors
Main.java:28: error: cannot find symbol
ConsumerThread consumerThread = new ConsumerThread(sharedQ, numberofElements);
^
symbol: class ConsumerThread
location: class Main
Main.java:28: error: cannot find symbol
ConsumerThread consumerThread = new ConsumerThread(sharedQ, numberofElements);
^
symbol: class ConsumerThread
location: class Main
Main.java:29: error: cannot find symbol
ProducerThread producerThread = new ProducerThread(sharedQ, numberofElements);
^
symbol: class ProducerThread
location: class Main
Main.java:29: error: cannot find symbol
ProducerThread producerThread = new ProducerThread(sharedQ, numberofElements);
^
symbol: class ProducerThread
location: class Main
4 errors
i've checked for duplicates but didnt find any, please help me out with this.