Java同步方法


/*
*银行取钱
*/
package com.fatiaoyu;

public class Account1 {
private int balance;

public Account1(int balance) {
	this.balance = balance;
}
public int getBalance() {
	return balance;
}

public synchronized void draw(int drawNum) {
	if(balance > drawNum) {
		System.out.println(Thread.currentThread().getName()+"取钱成功,取钱:"+drawNum);
		balance = balance - drawNum;
		System.out.println("当前余额是:"+balance);
	}else {
		System.out.println(Thread.currentThread().getName()+"取钱失败,余额不足");
	}
}

}

/*

  • 銀行取錢
    */
    package com.fatiaoyu;

public class DrawThread1 extends Thread {

private Account1 account;
private int drawNum;

public DrawThread1(Account1 account, int drawNum) {
	this.account = account;
	this.drawNum = drawNum;
}

public void run() {
	account.draw(drawNum);
}

}

/*

  • 继续取钱
    */
    package com.fatiaoyu;

import com.fatiaoyu.DrawThread1;

public class test_8 {

public static void main(String[] args) {

	Account1 account = new Account1(1000);
	DrawThread1 t1 = new DrawThread1(account, 600);
	DrawThread1 t2 = new DrawThread1(account, 500);
	DrawThread1 t3 = new DrawThread1(account, 500);
	DrawThread1 t4 = new DrawThread1(account, 800);

	t1.setName("取钱者1");
	t2.setName("取钱者2");
	t3.setName("取钱者3");
	t4.setName("取钱者4");

	t1.start();
	t2.start();
	t3.start();
	t4.start();

}

}

在这里插入图片描述

线程通信:

/*

  • bankAccount 类
    */
    package com.fatiaoyu;

public class Account {
private int balance; // 余额,简单演示,随便定义为int
boolean flag;

public Account(int balance) {
	this.balance = balance;
}

public int getBalance() {
	return balance;
}

public void setBalance(int balance) {
	this.balance = balance;
}

public synchronized void draw(int drawNum) {
	if (flag) {
		if (balance > drawNum) {
			System.out.println(Thread.currentThread().getName() + ",取钱成功,取钱:" + drawNum);
			balance = balance - drawNum;
			System.out.println(Thread.currentThread().getName() + ",当前余额是: " + balance);
		} else {
			System.out.println(Thread.currentThread().getName() + ",对不起,余额不足");
		}
		flag = false;
		notifyAll();
	} else {
		try {
			wait();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

public synchronized void deposit(int depositNum) {
	if (flag) {
		try {
			wait();
		} catch (Exception e) {
			e.printStackTrace();
		}
	} else {
		System.out.println(Thread.currentThread().getName() + ",存钱成功,存了:" + depositNum);
		balance = balance + depositNum;
		System.out.println(Thread.currentThread().getName() + ",当前余额是:" + balance);
		flag = true;
		notifyAll();
	}
}

}

/*

  • 再谈取钱
    */
    package com.fatiaoyu;

public class DepositThread extends Thread {

private Account account;
private int depositNum;

public DepositThread(Account account, int depositNum) {
	this.account = account;
	this.depositNum = depositNum;
}

public void run() {
	for (int i = 0; i < 100; i++) {
		account.deposit(depositNum);
	}
}

}

/*

  • 现场通信
    */
    package com.fatiaoyu;

public class ThScoket_test10 {

public static void main(String[] args) {
	Account account = new Account(1000);
	DrawThread thread1 = new DrawThread(account, 300);
	DrawThread thread2 = new DrawThread(account, 500);
	DepositThread thread3 = new DepositThread(account, 500);

	thread1.setName("取钱者1");
	thread2.setName("取钱者2");
	thread3.setName("存钱者");
	thread1.start();
	thread2.start();
	thread3.start();
}

}

打印结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值