有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Eclipse错误:无法找到或加载主类consumerProducer。消费者生产者

在我学习的过程中,我试着做一个书中的例子,我不断地得到这个错误。这是《Java入门》第1120页的消费者制作人Daniel Liang。每次编译时,标题中都会出现错误

package consumerProducer;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;

public class ConsumerProducer {
private static Buffer buffer = new Buffer();

public static void main(String[] args){
    //create threadpool with two objects
    ExecutorService executor = Executors.newFixedThreadPool(2);
    executor.execute(new ProducerTask());
    executor.execute(new ConsumerTask());
    executor.shutdown();
}//end main 

private static class ProducerTask implements Runnable{
    public void run(){
        try{
            int i = 1;
            while(true){
                System.out.println("Producer writes " + i);
                buffer.write(i++);
                //put thread into sleep mode
                Thread.sleep((int)(Math.random() * 10000));
            }//end while
        }//end try
        catch(InterruptedException ex){
            ex.printStackTrace();
        }//end catch 
    }//end run
}//end ProducerTask

//a task for reading and deleting an int from the buffer
private static class ConsumerTask implements Runnable{
    public void run(){
        try{
            while(true){
                System.out.println("\t\t\tConsumer reads " + buffer.read());
                //put thread to sleep 
                Thread.sleep((int)(Math.random() * 10000));
            }//end while
        }//end try

        catch(InterruptedException ex){
            ex.printStackTrace();
        }//end catch 
    }//end run
}//end Consumer task

//an inner class for buffer
private static class Buffer{
    private static final int CAPACITY = 1; //buffer size
    private java.util.LinkedList<Integer> queue = 
            new java.util.LinkedList<>();

    //create a new lock 
    private static Lock lock = new ReentrantLock();

    //create two conditions
    private static Condition notEmpty = lock.newCondition();
    private static Condition notFull = lock.newCondition();

    public void write(int value){
        lock.lock(); //acquire the lock
        try{
            while(queue.size() == CAPACITY){
                System.out.println("wait for notFull condition");
                notFull.await();
            }//end while

            queue.offer(value);
            notEmpty.signal(); //signal notEmpty ncondition
        }//end try 
        catch (InterruptedException ex){
            ex.printStackTrace();
        }//end catch 
        finally {
            lock.unlock(); //release the lock 
        }//end finally
    }//end write 

    public int read(){
        int value = 0;
        lock.lock();//acquire the lock 
        try{
            while(queue.isEmpty()){
                System.out.println("t\t\tWait for notEmpty condition");
                notEmpty.await();
            }//while

            value = queue.remove();
            notFull.signal(); //signal notFull condition
        }//end try
        catch(InterruptedException ex) {
            ex.printStackTrace();
        }//end catch 
        finally {
            lock.unlock();//release the lock 
            return value;
        }//end finally 
    }//end read 
}//end Buffer 
}//end class 

共 (0) 个答案