简体   繁体   English

Java多线程中的生产者使用者

[英]Producer consumer in java multithreading

I have my own implementation of producer and consumer in java multithreading. 我在Java多线程中有自己的生产者和使用者实现。 But consumer gets the product before producer puts the product. 但是消费者在生产者投入产品之前就获得了产品。 How to overcome this using wait and notify. 如何使用等待和通知来克服这个问题。

package prod;

public class InitProCon {

volatile static int size = 5;

public static void main(String[] args) {
    Consumer con = new Consumer("Consumer Getting", size);
    Producer pro = new Producer("Producer putting", size);
    Thread producer = new Thread(pro);
    Thread consumer = new Thread(con);
    producer.start();
    consumer.start();
}
}


package prod;

public class Product {

static String product;

public void putProduct(String product) {
    Product.product = product;
}

public String getProduct() {
    return product;
}
}




package prod;

public class Consumer implements Runnable {

String cname;
int size;

Consumer(String name, int size) {
    this.cname = name;
    this.size = size;
    System.out.println(cname);
}

@Override
public void run() {
    try {
        for (int i = 0; i < size; i++) {
            Product c = new Product();
            c.getProduct();
            System.out.println("Consumer got product" + i + ""
                    + c.getProduct());
        }
    } catch (Exception e) {
        Thread.currentThread().interrupt();
    }
}
}



package prod;

public class Producer implements Runnable {

String pname;
int size;

Producer(String name, int size) {
    this.pname = name;
    this.size = size;
    System.out.println(pname);
}

@Override
public void run() {
    try {
        for (int i = 0; i < size; i++) {
            Product p = new Product();
            p.putProduct("Consumer product");
            System.out.println("Producer put" + i);
        }
    } catch (Exception e) {
        Thread.currentThread().interrupt();
    }
}
}

You don't have different product contents, you only have one: 您没有不同的产品内容,只有一个:

package prod;
public class Product {
    static String product;
    public void putProduct(String product) {
        Product.product = product;

Notice the static : All products ( Product objects) share the same content. 注意static :所有产品( Product对象)共享相同的内容。 So when your producer produces a new product 因此,当您的生产者生产新产品时

        Product p = new Product();
        p.putProduct("Consumer product");

you set the content for all products. 您设置所有产品的内容。 So it may seem that the consumers get the same product but that is not quite correct. 因此, 似乎消费者获得了相同的产品,但这并不完全正确。 Every consumer gets a different object of type Product but all products share the same content. 每个消费者都有一个不同的“ Product ”类型对象,但是所有产品共享相同的内容。 By creating the new product you change the content of existing products also. 通过创建新产品,您还可以更改现有产品的内容。 When outputting you can't notice the difference because you only use an artificial index and the shared content (would you use a object specific value like `hashCode´ you could see that those are different objects). 在输出时,您不会注意到差异,因为您仅使用人工索引和共享内容(如果使用对象特定的值(例如“ hashCode”),则会发现它们是不同的对象)。

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

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