关于消费者.生产者,缓冲区的简单java实现
来源:互联网 发布:好听的名字知乎 编辑:程序博客网 时间:2024/05/29 14:34
package com.huawei.concurrent.test;
import java.util.LinkedList;
class Consumer extends Thread
private ProductTable productTable;
public Consumer(ProductTable productTable)
{
this.productTable = productTable;
}
public void run()
{
for (int i = 1; i <= 10; i++)
{
productTable.getProduct();
}
}
}
class Producer extends Thread
{
private Product p;
private ProductTable productTable;
public Producer(ProductTable productTable, Product p)
{
this.productTable = productTable;
this.p = p;
}
public void run()
{
for (int product = 1; product <= 10; product++)
{
productTable.addProduct(p);
}
}
}
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;
}
}
class ProductTable
{
private final int maxSize;
private LinkedList<Product> products = new LinkedList<Product>();
public ProductTable(int maxSize)
{
this.maxSize = maxSize;
}
public synchronized void addProduct(Product product)
{
while (products.size() >= maxSize)
{
try
{
wait();
}
catch (InterruptedException e)
{}
}
products.addLast(product);
System.out.println(product.getId() + " added");
notify();
}
public synchronized Product getProduct()
{
while (products.size() <= 0)
{
try
{
wait();
}
catch (InterruptedException e)
{}
}
Product product = (Product) products.removeFirst();
System.out.println(product.getId() + " removed");
notify();
return product;
}
}
public class MainT
{
public static void main(String[] args)
{
Product a = new Product(1, "a");
ProductTable queue = new ProductTable(5);
Producer producera = new Producer(queue, a);
Consumer consumer = new Consumer(queue);
new Thread(producera).start();
new Thread(consumer).start();
}
}
import java.util.LinkedList;
class Consumer extends Thread
private ProductTable productTable;
public Consumer(ProductTable productTable)
{
this.productTable = productTable;
}
public void run()
{
for (int i = 1; i <= 10; i++)
{
productTable.getProduct();
}
}
}
class Producer extends Thread
{
private Product p;
private ProductTable productTable;
public Producer(ProductTable productTable, Product p)
{
this.productTable = productTable;
this.p = p;
}
public void run()
{
for (int product = 1; product <= 10; product++)
{
productTable.addProduct(p);
}
}
}
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;
}
}
class ProductTable
{
private final int maxSize;
private LinkedList<Product> products = new LinkedList<Product>();
public ProductTable(int maxSize)
{
this.maxSize = maxSize;
}
public synchronized void addProduct(Product product)
{
while (products.size() >= maxSize)
{
try
{
wait();
}
catch (InterruptedException e)
{}
}
products.addLast(product);
System.out.println(product.getId() + " added");
notify();
}
public synchronized Product getProduct()
{
while (products.size() <= 0)
{
try
{
wait();
}
catch (InterruptedException e)
{}
}
Product product = (Product) products.removeFirst();
System.out.println(product.getId() + " removed");
notify();
return product;
}
}
public class MainT
{
public static void main(String[] args)
{
Product a = new Product(1, "a");
ProductTable queue = new ProductTable(5);
Producer producera = new Producer(queue, a);
Consumer consumer = new Consumer(queue);
new Thread(producera).start();
new Thread(consumer).start();
}
}
- 关于消费者.生产者,缓冲区的简单java实现
- 简单的生产者消费者模式java实现
- Java简单的生产者与消费者实现
- 生产者消费者多缓冲区实现
- java生产者消费者的实现
- 生产者-消费者的java实现
- 生产者消费者的java实现
- JAVA生产者消费者的实现
- java:一个生产者消费者模式的简单实现
- Java实现 简单的多线程“生产者-消费者”问题
- java 编码实现简单的生产者-消费者模式
- 关于Java多线程实现生产者和消费者的问题
- Java多线程实现简单生产者消费者模式
- 生产者、消费者简单实现
- 生产者消费者简单实现
- 生产者/消费者模式 (六) 环形缓冲区的实现
- 异步之生产者消费者模型--同步缓冲区的实现
- 生产者消费者 多线程 单个缓冲区 Win32API实现
- 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
- 关于多线程实现火车票售票模拟
- 问题总结
- Greatest Common Divisor(GCD) of Two Numbers
- 实施开发组队
- 关于消费者.生产者,缓冲区的简单java实现
- 强制的类型转换
- 修复数据库脚本
- linux内核返回错误参数
- "main" org.apache.hadoop.mapred.InvalidInputException: Input path does not exist: file
- C++中的临时对象(拷贝构造函数)(上)
- 关于dijit.form.Select中addOption()的一点注意事项
- 植物大战僵尸辅助之重叠植物
- Mozilla 免费送火狐手机 刺激开发者兴趣