0

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.

Turing85
  • 18,217
  • 7
  • 33
  • 58
Baig
  • 23
  • 1
  • 5
  • But it works fine in my eclipse – Baig Jan 07 '18 at 18:49
  • I've checked the link but it is not of much help. This thing works fine in local but when i try to submit it online, it says compile error- @azurefrog – Baig Jan 07 '18 at 18:56
  • Are you sure that all your public classes are each within their own file, and the file name matches the class name? – GhostCat Jan 07 '18 at 19:16
  • Yes, they are in the same package and the class names are same. The problem comes only in the online compiler- @GhostCat – Baig Jan 07 '18 at 19:19
  • Then we cant help with that. If your source code is correct, then that online tool has a problem. You want to get in contact with the people owning that service then. You see, the *core* thing is that other people need to be able to repro your problem. This community is not the help desk for that 3rd party service. – GhostCat Jan 07 '18 at 19:20

0 Answers0