如何为替代生产者和消费者方法编写 Java 多线程代码。它应该有 3 个生产者(P1、P2、P3)和 1 个消费者(C1)

How to Write Java multithreading Code for alternative producer and consumer approach. It should have 3 producers (P1,P2,P3) and 1 consumer(C1)

我能得到一个明确的方法来实现这个目标吗?我已经使用加入并等待替代生产者和消费者执行。这个问题是在采访中被问到的。他不喜欢我的解决方案。我还建议在同步块内使用循环来消耗/生产资源。

下面是预期的输出:

p1 c1 p3 c1 p2 c1 p2 c1 . . .

import java.util.concurrent.atomic.AtomicInteger;

public class TestClient {

    public static void main(String[] args) {

        ProducerConsumerUtilClass pcuc=new ProducerConsumerUtilClass();

        Thread producer1= new Thread(new Runnable() {

            @Override
            public void run() {
                while(true) {
                    try {
                        Thread.sleep(1000);
                        pcuc.produce();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

            }
        });

Thread producer2= new Thread(new Runnable() {

            @Override
            public void run() {
                while(true) {
                    try {
                        Thread.sleep(1000);
                        pcuc.produce();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

            }
        });
Thread producer3= new Thread(new Runnable() {

    @Override
    public void run() {
        while(true) {
            try {
                Thread.sleep(1000);
                pcuc.produce();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
});


Thread consumer1= new Thread(new Runnable() {

@Override
public void run() {
    while(true) {
        try {
            Thread.sleep(1000);
            pcuc.consume();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
});

        producer1.start();
        producer2.start();
        producer3.start();
        consumer1.start();



    }

}

 class ProducerConsumerUtilClass {

    Object obj= new Object();
    private volatile boolean  available;
    private AtomicInteger atomicInteger=null;

    public ProducerConsumerUtilClass() {
        this.available = false;;
        this.atomicInteger = new AtomicInteger(0);;
    }

    public void produce() {
        synchronized (obj) {
                while(available) {
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
                System.out.println("Produce By "+Thread.currentThread().getName()+"Value "+atomicInteger.getAndIncrement());
                this.available=true;
                obj.notifyAll();
        }

    }   

    public void consume() {
        synchronized (obj) {
                while(!available) {
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
                System.out.println("Consume By "+Thread.currentThread().getName()+"Value "+atomicInteger.getAndIncrement());
                this.available=false;
                obj.notifyAll();
        }

    }   


}