繁体   English   中英

Java中如何正确实现生产者-消费者

[英]how to properly implement producer - consumer in Java

我像这样实现了生产者消费者。

但它正在抛出错误。

我尝试使用这种使用锁的方法。 关联

class Testclass {
    Boolean isFresh = false;
    int count = 0;
    public synchronized void GET(String threadName){
        while(!isFresh){
            try {
                isFresh.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("GET method was called : " + count + " " + threadName);
        isFresh = false;
        isFresh.notify();

    }

    public synchronized void PUT(String threadName){
        while(isFresh){
            try{
                isFresh.wait();
            }catch( InterruptedException e){
                e.printStackTrace();
            }
        }
        count++;
        System.out.println("PUT method was called : " + count + " " + threadName);
        isFresh = true;
        isFresh.notify();
    }

}
class Producer implements Runnable{
    Testclass q;
    String name;
    Producer(Testclass q, String name){
        this.q = q;
        this.name = name;
    }
    public void run(){
        while(true){
            int time = (int)(Math.random() * 10000);

            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            q.PUT(this.name);
        }

    }
}

class Consumer implements Runnable{
    Testclass q;
    String name ;
    Consumer(Testclass q,String name){
        this.q = q;
        this.name = name;
    }
    public void run(){
        while(true){
            int time = (int)(Math.random() * 10000);

            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            q.GET(this.name);
        }

    }
}


public class Main {
    public static void main(String[] args){
        Testclass t = new Testclass();
        Thread t1 = new Thread(new Consumer(t, "consumer 1"));
        Thread t2 = new Thread(new Consumer(t, "consumer 2"));
        Thread t3 = new Thread(new Producer(t, "producer 1"));
        Thread t4 = new Thread(new Producer(t, "producer 2"));
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        try{
            t1.join();
        }catch (InterruptedException e){
            e.printStackTrace();
        }

    }



}

此实现引发以下错误。

请解释为什么所有线程都抛出 Illegal MonitorStateException?

PUT method was called : 1 producer 1
Exception in thread "Thread-2" java.lang.IllegalMonitorStateException: current thread is not owner
    at java.base/java.lang.Object.notify(Native Method)
    at Testclass.PUT(Main.java:34)
    at Producer.run(Main.java:54)
    at java.base/java.lang.Thread.run(Thread.java:832)
GET method was called : 1 consumer 1
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException: current thread is not owner
    at java.base/java.lang.Object.notify(Native Method)
    at Testclass.GET(Main.java:19)
    at Consumer.run(Main.java:76)
    at java.base/java.lang.Thread.run(Thread.java:832)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException: current thread is not owner
    at java.base/java.lang.Object.wait(Native Method)
    at java.base/java.lang.Object.wait(Object.java:321)
    at Testclass.GET(Main.java:12)
    at Consumer.run(Main.java:76)
    at java.base/java.lang.Thread.run(Thread.java:832)
PUT method was called : 2 producer 2
Exception in thread "Thread-3" java.lang.IllegalMonitorStateException: current thread is not owner
    at java.base/java.lang.Object.notify(Native Method)
    at Testclass.PUT(Main.java:34)
    at Producer.run(Main.java:54)
    at java.base/java.lang.Thread.run(Thread.java:832)

Process finished with exit code 0

我想知道为什么output会这样? 什么是正确的实施方式?

您应该使用BlockingQueue 然后你不必使用等待/通知甚至同步。

class Testclass {
    BlockingQueue<Integer> queue = new ArrayBlockingQueue(1);
    AtomicInteger count = new AtomicInteger(0);
    public void GET(String threadName) throws InterruptedException{
        Integer i = queue.take();
        count.getAndIncrement();
        System.out.println("GET method was called : " + count + " " + threadName);
    }

    public void PUT(String threadName) throws InterruptedException{

        int c = count.get();
        queue.put(c);
        count.getAndIncrement();
        System.out.println("PUT method was called : " + count + " " + threadName);
    }

}

我不确定你想对 count 做什么。 这很容易通过更改队列的大小来扩展。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM