生產者與消費者問題Java實現

生產者與消費者問題Java實現

 

產品類:

 

public class Product {

   

    private int id;

    private String value;

 

    public Product(int id,String value){

        this.id=id;

        this.value=value;

    }

 

    public int getId() {

        return id;

    }

 

    public void setId(int id) {

        this.id = id;

    }

 

    public String getValue() {

        return value;

    }

 

    public void setValue(String value) {

        this.value = value;

    }

   

}

 

 

公用緩衝池

import java.util.LinkedList;

 

public class ProductTable {

   

    // 緩衝池

    private LinkedList<Product> products = new LinkedList<Product>();

 

    // 生產

    public synchronized void produce(Product product) {

        while(products.size() >= 10) { // 容量限制為 10

            try {

                wait();

            }

            catch(InterruptedException e) {}

        }

        products.addLast(product);

        System.out.println(product.getId()+" added");

        notifyAll();

    }

 

    // 消費

    public synchronized Product consume() {

        while(products.size() <= 0) {

            try {

                wait();

            }

            catch(InterruptedException e) {}

        }

        Product product = (Product) products.removeFirst();

        System.out.println(product.getId()+" removed");

        notifyAll();

        return product;

    }

   

}

 

生產者:

 

public class Producer implements Runnable {

   

    private ProductTable productTable;

 

    public Producer(ProductTable productTable) {

        this.productTable = productTable;

    }

 

    public void run() {

    for (int i=0; i<5; i++) {

        Product p = new Product(i, "product " + "-" + i);

            productTable.produce(p);

            try {

                Thread.sleep((int) (Math.random() * 3000)); // 等待一個隨機的時間

            }

            catch (InterruptedException e) {

                e.printStackTrace();

            }

    }

    }

}

 

 

消費者:

public class Consumer implements Runnable {

   

    private ProductTable productTable;

 

    public Consumer(ProductTable productTable) {

        this.productTable = productTable;

    }

 

    public void run() {

        for (int i = 1; i <= 5; i++) {

            try {

                Thread.sleep((int) (Math.random() * 3000)); // 等待一個隨機的時間

            }

            catch (InterruptedException e) {

                e.printStackTrace();

            }

            productTable.consume();

        }

    }

}

 

測試類:

 

public class Test {

 

    public static void main(String[] args) {

   

        ProductTable queue = new ProductTable();

       

        // 生產者線程體

        Producer producer = new Producer(queue);

       

        // 消費者線程體

        Consumer consumer = new Consumer(queue);

       

        for (int i=0; i<2; i++) {

        Thread prod = new Thread(producer);

        prod.start();

        }

       

        for (int i=0; i<2;i++) {

        Thread consu = new Thread(consumer);

        consu.start();

        }

       

    }

   

}

 

 

 

 

發佈了103 篇原創文章 · 獲贊 5 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章