Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不在这里工作?_Java_Concurrent Programming_Java.util.concurrent - Fatal编程技术网

Java 为什么不在这里工作?

Java 为什么不在这里工作?,java,concurrent-programming,java.util.concurrent,Java,Concurrent Programming,Java.util.concurrent,为什么我会得到null?您还没有在main方法中初始化队列。我猜您会得到一个NullPointerException,因为从来没有创建队列对象,并且生产者和消费者引用的队列为null 您还没有在main方法中初始化队列。我猜您会得到一个NullPointerException,因为从来没有创建队列对象,并且生产者和消费者引用的队列为null 即使正确初始化队列,您的实现仍然存在一个主要问题。如果Consumer线程在队列为空时尝试获取项目(根据您的代码,这是完全可能的),则Consumer线程进

为什么我会得到null?

您还没有在main方法中初始化队列。我猜您会得到一个NullPointerException,因为从来没有创建队列对象,并且生产者和消费者引用的队列为null

您还没有在main方法中初始化队列。我猜您会得到一个NullPointerException,因为从来没有创建队列对象,并且生产者和消费者引用的队列为null

即使正确初始化队列,您的实现仍然存在一个主要问题。如果
Consumer
线程在队列为空时尝试获取项目(根据您的代码,这是完全可能的),则
Consumer
线程进入无限期等待,并持有对象的锁<代码>生产者线程永远不能将项目放入队列。整个过程将停止。

即使您正确初始化了队列,您的实现仍然存在一个重大问题。如果
Consumer
线程在队列为空时尝试获取项目(根据您的代码,这是完全可能的),则
Consumer
线程进入无限期等待,并持有对象的锁<代码>生产者线程永远不能将项目放入队列。整个过程将停止。

您在这里遇到了什么具体错误?您所说的“为什么异常不包含null”是什么意思?您在这里得到的具体错误是什么?你说的“为什么异常不包含null”是什么意思?我应该怎么做,对不起,我没有Java方面的经验,这是首先要学习的。我建议你读一本关于Java基础知识的书。静态NaveSQ队列=新NaveSQ();我应该怎么做,抱歉,我没有Java方面的经验,这是我首先要学习的。我建议你读一本关于Java基础知识的书。静态NaveSQ队列=新NaveSQ();
class NaiveSQ<E> {
    boolean putting = false;
    E item = null;

    public synchronized E take() throws InterruptedException {
        while (item == null)
            wait();
        E e = item;
        item = null;
        notifyAll();
        return e;
    }

    public synchronized void put (E e) throws InterruptedException {
        if (e == null)
            return;
        while (putting)
            wait();
        putting = true;
        item = e;
        notifyAll();
        while (item != null)
            wait();
        putting = false;
        notifyAll();
    }
}

class Producer implements Runnable {
    int id = -1;
    int limit = 1;

    Producer(int x) {
        id = x;
    }

    public void run() {
        System.out.printf("I am producer number %d\n", id);
        for (int i=0; i<limit; i++) {
            Integer I = new Integer(i);
            try {
                Test.queue.put(I);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

class Consumer implements Runnable {
    int id = -1;

    Consumer(int x) {
        id = x;
    }

    public void run() {
        try {
            Integer I = Test.queue.take();
            System.out.printf(
                "I am consumer number %d - I read %d\n", id, I.intValue());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

public class Test{
    static NaiveSQ<Integer> queue;
    public static void main (String [] args){
        System.out.println("hello from Java");
        Thread p = new Thread(new Producer(1));
        p.start();
        for (int i=0; i<1; i++) {
            Thread c = new Thread(new Consumer(i));
            c.start();
        }
    }
};
hello from Java
I am producer number 1
null
null