在创建线程时找不到符号,编译错误

时间:2018-01-07 18:46:38

标签: java multithreading java-threads thread-synchronization blockingqueue

我遇到了这个问题,它在我的IDE中运行正常,但在我提交代码的在线编译器中有编译错误。请帮忙。

编写一个多线程Java程序来实现Producer和Consumer。从main方法跨越生产者和消费者的两个线程。使用wait()和notify()方法在线程之间进行协调是容易出错的,因此在本例中我们将使用阻塞队列,该队列用于在多线程之间传递消息。在此问题中,生产者将数字从0 - N插入阻塞队列,并且消费者在此队列上侦听任何信息。插入数据后,消费者将获取消息或号码并在控制台上打印。

示例输入:

**5**

示例输出:

Consume value: 0
Consume value: 1
Consume value: 2
Consume value: 3
Consume value: 4    

以下是代码:

**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();

    }
}

主题类
    公共类ConsumerThread扩展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);
            }
        }
    }
}

错误是

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

我已经检查了重复但没找到,请帮帮我。

0 个答案:

没有答案