多线程----线程通信

线程通信方法

Java实现线程通信的方法
wait()方法
挂起当前线程,并释放共享资源的锁
notify()方法
在因调用该对象的wait()方法而阻塞的线程中随机选择一个解除阻塞,但要等到获得锁后才可真正执行
notifyAll()方法
将因调用该对象的wait()方法而阻塞的所有线程一次性全部解除阻塞


案例1--存钱取钱

需求:对前面存钱、取钱案例进行线程通信,保证存一笔,取一笔,不存 就不能取。

package com.qf.day22_1;
public class Demo2 {
	public static void main(String[] args) {
		bankCard card = new bankCard();
		addMoney add = new addMoney(card);
		subMoney sub = new subMoney(card);
		
		Thread zhangsan = new Thread(add,"张三");
		Thread lisi = new Thread(add,"李四");
		Thread xiaoming = new Thread(sub,"小明");
		Thread xiaohong = new Thread(sub,"小红");
		
		zhangsan.start();
		lisi.start();
		xiaohong.start();
		xiaoming.start();
		
	}
}
/**
 * 银行卡
 * @author Administrator
 *
 */
class bankCard{
	double money;
	boolean flage=false;//false 为没钱,true为有钱
	/**
	 * 存钱
	 * @param d
	 */
	public synchronized void SaveMoey(double d) {
		
		while (flage) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		money=money+d;
		System.out.println(Thread.currentThread().getName()+"存了"+d+",卡上余额"+money);
		
		flage = true;
		this.notifyAll();
	}
	/**
	 * 取钱
	 * @param d
	 * @return
	 */
	public synchronized void recMoney(double d) {
		while (!flage) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if (money>=d) {
			money = money-d;
			System.out.println(Thread.currentThread().getName()+"取了"+d+",卡上余额"+money);
			
		}else {
			System.out.println("余额不足");
			
		}
		flage = false;
		this.notifyAll();
	}
}

/**
 * 存钱类
 * @author Administrator
 *
 */
class addMoney implements Runnable{
	bankCard card;
	public addMoney(bankCard card) {
		// TODO Auto-generated constructor stub
		this.card = card;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++) {
//			synchronized (card) {
//				this.card.money += 1000;
//				System.out.println(Thread.currentThread().getName()
//						+ "存了1000,卡上余额" + this.card.money);
//			}
			this.card.SaveMoey(1000);
		}
	}
	
}
/**
 * 取钱类
 * @author Administrator
 *
 */
class subMoney implements Runnable{

	bankCard card;
	public subMoney(bankCard card) {
		// TODO Auto-generated constructor stub
		this.card = card;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++) {
//			synchronized (card) {
//				if (this.card.money >= 1000) {
//
//					this.card.money -= 1000;
//					System.out.println(Thread.currentThread().getName()
//							+ "取了1000,卡上余额" + this.card.money);
//				} else {
//					System.out.println("余额不足");
//					i--;
//				}
			
		this.card.recMoney(1000);
			
		}
	}
	
}

案例2--输入输出字母

需求:按照26个字母表顺序,依次 输入一个字母,输出一个字母。

package com.qf.day22_1;

public class Demo3 {
	public static void main(String[] args) {
		LetterCon letterCon = new LetterCon();
		InputThread in = new InputThread(letterCon);
		OutputThread out = new OutputThread(letterCon);
		Thread in1 = new Thread(in,"线程1");
		Thread out1 = new Thread(out,"线程2");
		Thread in2 = new Thread(in,"线程3");
		Thread out2 = new Thread(out,"线程4");
		in1.start();
		in2.start();
		out1.start();
		out2.start();
	}
}

class LetterCon{
	char let;
	boolean flage=false;//true 为 有数据,false  为无数据
	
	public synchronized void input(char c) {
		
		while (flage) {
			try {
				this.wait();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		System.out.println(Thread.currentThread().getName()+"输入"+c);
		let = c;
		
		flage = true;
		this.notifyAll();
		
	}
	public synchronized void output(){
		while (!flage) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(Thread.currentThread().getName()+"输出"+let);
		
		flage = false;
		this.notifyAll();
	}
}

class InputThread implements Runnable{
	private LetterCon letterCon ;
	public InputThread(LetterCon letterCon){
		this.letterCon = letterCon;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (char i = 'A'; i <='Z'; i++) {
			letterCon.input(i);
		}
	}
	
}

class OutputThread implements Runnable{
	private LetterCon letterCon;
	public OutputThread(LetterCon letterCon){
		this.letterCon=letterCon;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 26; i++) {
			letterCon.output();
		}
	}
	
}

案例三----生产者消费者问题

需求
生产者可以生产面包 ,面包放入一个容器中,容器大小为6,消费者可以从容器中消费,如果没有面包是不能消费的,要通知生产者生产,如果容器中已经满了,也不能再生产,要通知消费者去消费。

package com.qf.day22_1;

public class Product_ConsumeDemo {
	public static void main(String[] args) {
		BreadCon breadCon = new BreadCon();
		ProductThread p = new ProductThread(breadCon);
		ConsumThread c = new ConsumThread(breadCon);
		
		Thread zhangsan = new Thread(p,"张三");
		Thread lisi = new Thread(p,"李四");
		Thread xiaoming = new Thread(c, "小明");
		Thread xiaohong = new Thread(c, "小红");
		zhangsan.start();
		lisi.start();
		xiaoming.start();
		xiaohong.start();
		
	}
	
	
}


class Bread{
	private String Pro_name;
	private int bread_Id;
	public String getPro_name() {
		return Pro_name;
	}
	public void setPro_name(String pro_name) {
		Pro_name = pro_name;
	}
	public int getBread_Id() {
		return bread_Id;
	}
	public void setBread_Id(int bread_Id) {
		this.bread_Id = bread_Id;
	}
	public Bread(String pro_name, int bread_Id) {
		super();
		Pro_name = pro_name;
		this.bread_Id = bread_Id;
	}
	
}

class BreadCon{
	Bread[] breads = new Bread[6];
	int length = -1;
	//生产方法
	public synchronized void product(Bread bread){
		while(length>=5){
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		length++;
		breads[length]=bread;
		System.out.println(Thread.currentThread().getName()+"生产了"+bread.getBread_Id()+"面包");
		
		this.notifyAll();
		
	}
	
	//消费方法
	public synchronized void consum() {
		
		while(length<=-1){
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		Bread b = breads[length];
		length--;
		System.out.println(Thread.currentThread().getName()+"消费了"+b.getBread_Id()+"面包 ");
		this.notifyAll();
	}
	
}

class ProductThread implements Runnable{
	BreadCon breadCon;
	public ProductThread(BreadCon breadCon){
		this.breadCon = breadCon;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 30; i++) {
			Bread bread = new Bread(Thread.currentThread().getName(), i);
			breadCon.product(bread);
		}
	}
	
}

class ConsumThread implements Runnable{
	BreadCon breadCon;
	public ConsumThread(BreadCon breadCon){
		this.breadCon = breadCon;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 30; i++) {
			breadCon.consum();
		}
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值