一
/*
*银行取钱
*/
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();
}
}
打印结果: