生产者、消费者(线程Demo)

package com.huawei.nebula.acl.dao.test;
import java.util.ArrayList;
import java.util.List;

/**
* 商品队列类 生产和消费
*
* @author lKF22533
*
*/
public class ProductQueue {
// 队列最大值
private int maxCount;
// 队列
private List<Product> list;

public ProductQueue() {
super();
// TODO Auto-generated constructor stub
}

public ProductQueue(int maxCount) {
super();
this.maxCount = maxCount;
list = new ArrayList<Product>(maxCount);
}

/**
* 生产
*/
public synchronized void production(Product product) {
while (isFull()) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.list.add(product);
System.out.println(Thread.currentThread().getName()
+ " 商品: " + product.getId());
notifyAll();
}

/**
* 消费
*/
public synchronized void consume() {
while (isEmpty()) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Product product = list.get(list.size() - 1);
list.remove(product);
System.out.println(Thread.currentThread().getName()
+ " 商品: " + product.getId());
notifyAll();
}

private boolean isFull() {
if (this.maxCount == list.size()) {
System.out.println("队列已满,准备消费...");
return true;
}
return false;
}

private boolean isEmpty() {
if (list.size() == 0) {
System.out.println("队列已空,准备生产...");
return true;
}
return false;
}
}

package com.huawei.nebula.acl.dao.test;
/**
* main
* @author lKF22533
*
*/
public class ThreadDemo {
public static void main(String[] args) {
ProductQueue queue = new ProductQueue(10);
for (int i = 0; i < 10; i++) {
Production production = new Production(queue);
new Thread(production, "生产线程" + i).start();
}

for (int i = 0; i < 10; i++) {
Consume consume = new Consume(queue);
new Thread(consume, "消费线程" + i).start();

}
}
}

/**
* 生产类
*
* @author lKF22533
*
*/
class Production implements Runnable {
private static int id = 1;
private ProductQueue queue;

public Production(ProductQueue queue) {
super();
this.queue = queue;
}

@Override
public void run() {
while (true) {
Product product = new Product(id++, "product" + id);
queue.production(product);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

/**
* 消费类
*
* @author lKF22533
*
*/
class Consume implements Runnable {
private ProductQueue queue;

public Consume(ProductQueue queue) {
super();
this.queue = queue;
}

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

package com.huawei.nebula.acl.dao.test;
/**
* 商品实体类
* @author lKF22533
*
*/
public class Product {
private int id;
private String name;
//get/set
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值