线程同步实例

 

class Demo{ 
	public static void main(String[] args) throws InterruptedException {
		//创建描述动物行为的对象
		AnimalDescribe animal = new AnimalDescribe();
		
		//鸟类与鱼类分两个线程循环向动物描述对象中添加描述信息
		Fish fish = new Fish(animal);
		Bird brid = new Bird(animal);
		Beast beast = new Beast(animal);
		
		for (int i = 0; i <= 10; i++) {
			new Thread(fish).start();
			new Thread(brid).start();
			new Thread(beast).start();
			new Thread(animal).start();
		}
		
		animal.flag = 1;
		synchronized (animal) {
			animal.notifyAll();
		}
    }
}
//动物描述类,主要用来描述一个类动物的行为
class AnimalDescribe implements Runnable{
	int flag = 100;
	private int runCount = 45;
	private String typeName;//动物的种类
	private String action;  //动物的行为

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}
 
	//打印当前动物的种类和对应的行为
	public synchronized void print(){
		try {
			while (flag != 0) wait();
				System.out.println(Thread.currentThread().getName() + "\t--显示--" + typeName + ":" + action);
   
				if (typeName.equals("鸟")) {
					flag = 2;
				}
				else if (typeName.equals("鱼")) {
					flag = 3;
				}
				else if (typeName.equals("兽")) {
					flag = 1;
				} 
				notifyAll();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			print();
		}
	}
}
abstract class Animal implements Runnable {
	public AnimalDescribe animal = null;//保存一个动物描述对象
	public int runCount = 15;

	public Animal(AnimalDescribe animal) {

		this.animal = animal;
	}

	public void addData(int index, String typeName, String action){

		try {
			while (animal.flag != index) animal.wait();
    
			animal.setTypeName(typeName);
			animal.setAction(action);
			System.out.println(Thread.currentThread().getName() + "\t-添加数据完成!");
			animal.flag = 0;
			animal.notifyAll();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
	}
}
//鸟类
class Bird extends Animal {

	public Bird(AnimalDescribe animal) { 
		super(animal);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			synchronized (animal) {
				addData(1, "鸟", "在天上飞!");   
			}
		}
	}
}
//鱼类
class Fish extends Animal {

	public Fish(AnimalDescribe animal) {
		super(animal);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			synchronized (animal) {
				addData(2, "鱼", "在水里游!"); 
			}
		}
	}
}
//兽类
class Beast extends Animal {

	public Beast(AnimalDescribe animal) {
		super(animal);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			synchronized (animal) {
				addData(3, "兽", "在地上跑!");
			}
		}
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值