通过wait,notify等实现线程通讯
package org.thread.demo.threadcommunication;
/**
* 银行账号类
*
* */
public class Account {
//账号
private String account;
//密码
private String password;
//余额
private volatile double amount;
//定义标识 是不是有钱
private boolean flag = true;
/**
* 取钱
* */
public synchronized void suspend(double money) {
try {
Thread.sleep(1000);
if(flag) {
if(this.getAmount()>=money) {
this.setAmount(this.getAmount() - money);
System.out.println(Thread.currentThread().getName()+"取款成功");
if(this.getAmount()<=0) {
this.flag = false;
}
}else {
System.out.println(Thread.currentThread().getName()+"余额不足,取款失败");
//通知其他线程打钱 小头爸爸 二叔 王叔
this.notifyAll();
//自己的线程进行等待
this.wait();
}
}else {
//通知其他线程打钱 小头爸爸 二叔 王叔
this.notifyAll();
//自己的线程进行等待
this.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 存钱
* */
public synchronized void susume(double money) {
try {
Thread.sleep(1000);
if(flag) {
//卡里有钱小明取钱
this.notifyAll();
//自己等待
this.wait();
}else {
//卡里没钱 存款
this.setAmount(money+this.getAmount());
flag=true;
System.out.println(Thread.currentThread().getName()+"存款成功");
this.notifyAll();
//自己等待
this.wait();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public String toString() {
return "Account [account=" + account + ", password=" + password + ", amount=" + amount + "]";
}
}
package org.thread.demo.threadcommunication;
/**
* 线程通讯
* 当卡了余额不足时候,通知其他线程存钱
* 当线程存款完成后,通知其他线程去取现
* wait notify线程通讯
* */
public class Test {
public static void main(String[] args) {
//创建账户
Account account = new Account();
account.setAccount("1");
account.setPassword("1");
account.setAmount(10000);
//模拟三个线程 一个取钱 三个存钱
SuspendThread suspendThread = new SuspendThread(account,10000);
suspendThread.setName("大头儿子");
suspendThread.start();
SusumeThread susumeThread0 = new SusumeThread(account,10000);
susumeThread0.setName("小头爸爸");
SusumeThread susumeThread1 = new SusumeThread(account,10000);
susumeThread1.setName("二叔");
SusumeThread susumeThread2 = new SusumeThread(account,10000);
susumeThread2.setName("老王");
susumeThread1.start();
susumeThread0.start();
susumeThread2.start();
}
}
package org.thread.demo.threadcommunication;
/**
*
* 取现*/
public class SuspendThread extends Thread{
///定义成员变量,同步到内存中
private Account account;
private double money;
public SuspendThread(Account account,double money) {
this.account = account;
this.money = money;
}
//不断的取钱
@Override
public void run() {
while(true) {
account.suspend(money);
}
}
}
package org.thread.demo.threadcommunication;
/**
* 存钱
* */
public class SusumeThread extends Thread{
///定义成员变量,同步到内存中
private Account account;
private double money;
public SusumeThread(Account account,double money) {
this.account = account;
this.money = money;
}
//不断的存钱
@Override
public void run() {
while(true) {
account.susume(money);;
}
}
}