`
jxqc_job
  • 浏览: 529 次
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
多线程Demo--生产者与消费者 多线程demo--生产者与消费者
java多线程编程:果农摘水果,人们吃水果(生产者与消费者)
1. Fruit.java
package com.thread.test;

import java.util.Random;

public class Fruit {
	int id; //水果编号
	int number; //水果编号计数器
	String variety; //水果品种
	String[] varietys = "苹果,桃子,梨子,香蕉,西瓜,荔枝,葡萄".split(","); //水果品种数组
	public Fruit(){
		this.variety = this.varietys[new Random().nextInt(7)];
		this.id = ++this.number;
	}
}

2. FruitBasket.java
package com.thread.test.copy;

public class FruitBasket {
	Fruit[] fruits = new Fruit[10]; // 水果筐的容量
	int x; // 下一个水果放置的位置
	int num; // 水果编号

	public FruitBasket() {
		x = 0;
		num = 0;
		System.out.println("水果筐的容量为:" + fruits.length);
	}

	public boolean isEmpty() {
		return x == 0 ? true : false;
	}

	public boolean isFull() {
		return x == fruits.length ? true : false;
	}

	/**
	 * 放水果
	 * 
	 * @throws InterruptedException
	 */
	public synchronized void putFruit(String name) {
		if (isFull()) {
			try {
				this.wait();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			return;
		}
		fruits[x] = new Fruit(); // 放一个水果
		System.out
				.println(name + "放了一个" + fruits[x].variety + ":编号为" + (++num)); // 输出这个苹果信息
		fruits[x].id = num; // 给这个水果设置个编号
		x = x + 1;
//		System.out.println("----------框中剩余的水果数为:" + x);
		display();
		this.notify();
	}

	private void display() {
		for(int i = 0; i < fruits.length; i++){
			Fruit fruit = fruits[i];
			if(fruit != null){
				System.out.print("No." + fruits[i].id + ":" + fruits[i].variety + "\t\t");
			}else{
				System.out.print("【" + (i + 1 ) + "】" + "\t\t");
			}
		}
		System.out.println();
	}

	/**
	 * 吃水果
	 * 
	 * @throws InterruptedException
	 */
	public synchronized void popFruit(String name) {
		if (isEmpty()) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return;
		}
		// 水果筐中最上面的一个水果的位置
		x--;
		// 输出小孩吃的该水果的信息
		System.out.println(name + "把编号为:No." + fruits[x].id + "的" + fruits[x].variety + "给吃了");
		// 小孩拿出水果筐中最上面的一个水果吃
		fruits[x] = null;
		// 水果筐中剩余的水果数量
//		System.out.println("**********框中剩余的水果数为:" + x);
		display();
		this.notify();
	}

}



3. Farmer.java
package com.thread.test;

import java.util.Random;

public class Farmer implements Runnable{
	private FruitBasket fb; //水果
	private String name; //果农的姓名
	
	public Farmer(String name, FruitBasket fb){
		this.name = name;
		this.fb = fb;
	}
	@Override
	public void run(){
		
		while(true){
			fb.putFruit(name);
			int t = new Random().nextInt(5000);
//			System.out.println(name+":"+Thread.currentThread().getName() + "休眠" + t
//					+ "毫秒");
			try {
				Thread.sleep(t);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}


4. XiaoHai.java
package com.thread.test;

import java.util.Random;

public class XiaoHai implements Runnable{
	private FruitBasket fb; //水果
	private String name; //小孩的姓名
	public XiaoHai(String name, FruitBasket fb){
		this.fb = fb;
		this.name = name;
	}
	@Override
	public void run() {
		while(true){
			fb.popFruit(name);
			int t = new Random().nextInt(5000);
//			System.out.println(name+":"+Thread.currentThread().getName() + "休眠" + t
//					+ "毫秒");
			try {
				Thread.sleep(t);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}


5. TestDemo.java
package com.thread.test;

public class TestDemo {
	public static void main(String[] args) {
		FruitBasket fb = new FruitBasket();
		Farmer f = new Farmer("张三",fb);
		Farmer f2 = new Farmer("李四",fb);
		Farmer f3 = new Farmer("王五",fb);
		
		Thread tf = new Thread(f);
		Thread tf2 = new Thread(f2);
		Thread tf3 = new Thread(f3);
		
		XiaoHai xh = new XiaoHai("悟空", fb);
		XiaoHai xh2 = new XiaoHai("八戒", fb);
		XiaoHai xh3 = new XiaoHai("唐僧", fb);
		
		Thread txh = new Thread(xh);
		Thread txh2 = new Thread(xh2);
		Thread txh3 = new Thread(xh3);
				
		tf.start();
		tf2.start();
		tf3.start();
		
		txh.start();
		txh2.start();
		txh3.start();
		
	}
}
 
Global site tag (gtag.js) - Google Analytics