<span style="font-size:18px;">import java.util.ArrayList;
import java.util.List;
/**
* 仓库类,用储存商品,联系生产者和消费者
* @author hanwen
*
*/
public class Warehouse {
//定义一个集合,用来存储生产的产品
private List<String> products=new ArrayList<String>();
/**
* 生产者生产商品,存储到仓库里
*/
public void addproduct(String stringname){
products.add(stringname);//想仓库里面添加产品
}
/**
* 消费者消费商品
*/
public String getProduct(){
//判断仓库是不是为空
if(!products.isEmpty()){
//从仓库中取产品,返回给消费者
String stringname=products.remove(0);
return stringname;
}else{
System.out.println("仓库为空,请及时生产产品...");
return "";
}
}
/**
* 获取库存余量
*
* @return
*/
public int getSize(){
//返回库存余量
return products.size();
}
}</span><span style="font-size:18px;">/**
* 消费者线程类
*
* @author Administrator
*
*/
public class CustomerThread implements Runnable {
// 属性
private Warehouse wh; // 仓库对象
private String name; // 消费者姓名
// 构造函数传递参数
public CustomerThread(Warehouse wh, String name) {
super();
this.wh = wh;
this.name = name;
}
// 消费产品
@Override
public void run() {
while (true) {
synchronized (wh) {
if (wh.getSize() == 0) {
try {
System.out.println("仓库为空,消费者" + name + "进入等待状态........");
wh.wait();
System.out.println("线程等待结束,重新启动");
} catch (InterruptedException e) {
e.printStackTrace();
}
} else if (wh.getSize() > 0) {
// 取走商品,
String stringname = wh.getProduct();
System.out.println("消费者" + name + "取走了产品" + stringname);
}
}
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}</span><span style="font-size:18px;">import java.util.Random;
public class ProductThread implements Runnable {
// 定义一个仓库对象
private Warehouse wh;
// 有参数的构造方法
public ProductThread(Warehouse wh) {
super();
this.wh = wh;
}
/**
* 开始生产产品
*/
@Override
public void run() {
while (true) {
// 自动的随机产生产品
String stringname = "p" + (new Random()).nextInt(1000);
synchronized (wh) {
// 把产品放进仓库
wh.addproduct(stringname);
// 提示信息
System.out.println("仓库里面已经存入商品" + stringname);
//唤醒线程,让他继续执行
//wh.notify();
wh.notifyAll();
}
try {
// 睡一会
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}</span><span style="font-size:18px;">/**
* 测试类
* @author Administrator
*
*/
public class TestDemo {
public static void main(String[] args) {
//实例化仓库对象
Warehouse wh=new Warehouse();
//实例化生产者对象
ProductThread pt=new ProductThread(wh);
//实例化消费者对象
CustomerThread ct=new CustomerThread(wh,"小明");
CustomerThread ct1=new CustomerThread(wh,"小花");
CustomerThread ct2=new CustomerThread(wh,"小灰灰");
CustomerThread ct3=new CustomerThread(wh,"小飞飞");
//建线程类进行包装
Thread t=new Thread(pt);
Thread t2=new Thread(ct);
Thread t3=new Thread(ct1);
Thread t4=new Thread(ct2);
Thread t5=new Thread(ct3);
//启动线程
t.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}</span>线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其他某个线程中断当前线程,或者已超过某个实
际时间量。
⑥、wait() 和 notify() 方法(包括上述的所有方法,下同) 都是 Object 类的最终方法,所以每个类默认都拥有该方 法。为确保 wait() 和 notify() 的功能,在执行之前必须要先获得互斥锁,即一定要和 synchronized 一起使用。④、当前线程时间片用完了,调用当前线程的 yield() 方法。
4)、阻塞状态(Blocked):线程由于某些事件放弃CPU使用权,暂停运行。直到线程重新进入可运行
状态,才有机会转到运行状态。阻塞状态分为如下几种:
③、其它阻塞 – 线程执行 sleep() 或 join() 或发出 I/O 请求。
5)、死亡状态(Dead):run、main() 方法执行结束,或者因异常退出了 run() 方法,线程进入死亡状
态,不可再次复生。
原文地址:http://blog.csdn.net/han_wen2015/article/details/46592999