黑马程序员_王康 java多线程

package test;

/**
 * 
 * @author 黑马_王康
 *
 */
public class CheckInOut {

	public static void main(String[] args) {
		BankCard card = new BankCard();
		Parent px = new Parent(card, "爸爸", 1500, 500);
		Parent py = new Parent(card, "妈妈", 1000, 800);
		Parent pz = new Parent(card, "爷爷", 800, 1000);
		Children ca = new Children(card, "大女儿", 400, 600);
		Children cb = new Children(card, "二女儿", 300, 600);
		Children cc = new Children(card, "三儿子", 500, 600);
		new Thread(px).start();
		new Thread(py).start();
		new Thread(pz).start();
		new Thread(ca).start();
		new Thread(cb).start();
		new Thread(cc).start();
	}
}

class BankCard {
	int sum = 0;

	// 存款
	public synchronized void save(String name, int count) {
		// 如果存了足够的钱就不在存了
		while (sum > 5000) {
			try {
				System.out.println(name + "\t存款: 发现钱够了");
				// 等待,并且从这里退出push()
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		// 注意,notifyAll()以后,并没有退出,而是继续执行直到完成。
		this.sum += count;
		System.out.println(name + "\t存入了 [¥" + count + "]\t余额 [¥" + this.sum + "]");
		
		// 因为我们不确定有没有线程在wait(),所以我们既然存了钱,就唤醒有可能等待的孩子,让他们醒来,准备取款
		notifyAll();
		System.out.println("\t" + name + "告诉孩子存钱了");
	}

	// 取款
	public synchronized void cost(String name, int count) {
		// 如果钱不够了,就不再取款
		while (sum < count) {
			try {
				System.out.println(name + "\t取款: 等钱花" + count);
				// 等待,并且从这里退出pop()
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		// 注意,notifyAll()以后,并没有退出,而是继续执行直到完成。
		this.sum -= count;
		System.out.println(name + "\t取走了 [¥" + count + "]\t余额 [¥" + this.sum + "]");
		
		// 因为我们不确定有没有线程在wait(),所以我们既然消费了产品,就唤醒有可能等待的生产者,让他们醒来,准备生产
		notifyAll();
		System.out.println("\t" + name + "告诉父母取钱了");
	}
}


class Parent implements Runnable {
	
	BankCard card = null;// 银行卡
	String name;// 姓名
	int count;// 存款数量
	int interval;// 存款时间间隔

	Parent(BankCard card, String name, int count, int interval) {
		this.card = card;
		this.name = name;
		this.count = count;
		this.interval = interval;
	}

	public void run() {
		while (true) {
			card.save(name, count);
			try {
				Thread.sleep(interval);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

class Children implements Runnable {

	BankCard card = null;// 银行卡
	String name;// 姓名
	int count;// 取款数量
	int interval;// 取款时间间隔

	Children(BankCard card, String name, int count, int interval) {
		this.card = card;
		this.name = name;
		this.count = count;
		this.interval = interval;
	}


	public void run() {
		while (true) {
			//int count = (int) (Math.random() * degree);
			card.cost(name, count);
			try {
				Thread.sleep(interval);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值