Java中线程之间需要一些协调通信,来共同完成一件任务。第一种方式使用Object类中相关的方法有两个notify方法和三个wait方法:
package com.farsight.thread5;
class Account{
private String accountNo;
private double balance;
private boolean flag = false;
public Account(){ }
public Account(String accountNo, double balance){
this.accountNo = accountNo;
this.balance = balance;
}
public String getAccountNo() {
return accountNo;
}
public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}
public double getBalance() {
return balance;
}
// 设置取钱方法,因为账户余额不允许随便修改,所以专门实现存款方法
public synchronized void draw(double drawAmount){
try{
// 如果flag 为假,表明账户没有人存钱进去,取钱方法阻塞。
if(!flag){
wait();
}
else{
// 执行取钱操作
System.out.println(Thread.currentThread().getName()
+ " 取钱 :" + drawAmount);
balance -= drawAmount;
System.out.println("账户余额:" + balance);
// 将存款标识设为false
flag = false;
notifyAll();
}
}
catch(InterruptedException ex){
ex.printStackTrace();
}
}
// 设置存钱方法
public synchronized void deposit(double depositAmount){
try{
if(flag){
wait();
}
else{
// 执行存款操作
System.out.println(Thread.currentThread().getName() +
" 存款:" + depositAmount);
balance += depositAmount;
System.out.println("账户余额: " + balance);
// 将存款标识设为true
flag = true;
// 唤醒其他所有的线程
notifyAll();
}
}
catch(InterruptedException ex){
ex.printStackTrace();
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((accountNo == null) ? 0 : accountNo.hashCode());
long temp;
temp = Double.doubleToLongBits(balance);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + (flag ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Account other = (Account) obj;
if (accountNo == null) {
if (other.accountNo != null)
return false;
} else if (!accountNo.equals(other.accountNo))
return false;
if (Double.doubleToLongBits(balance) != Double
.doubleToLongBits(other.balance))
return false;
if (flag != other.flag)
return false;
return true;
}
}
class DrawThread extends Thread{
// 模拟用户账户
private Account account;
private double drawAmount;
public DrawThread(String name, Account account, double drawAmount){
super(name);
this.account = account;
this.drawAmount = drawAmount;
}
public void run(){
// 重复执行取钱动作
for(int i = 0; i < 100; i++){
account.draw(drawAmount);
}
}
}
class DepositThread extends Thread{
private Account account;
private double depositAmount;
public DepositThread (String name, Account account, double depositAmount){
super(name);
this.account = account;
this.depositAmount = depositAmount;
}
public void run(){
// 重复执行100次存钱的方法
for(int i = 0; i < 100;i++){
account.deposit(depositAmount);
}
}
}
public class ThreadCommunicateDemon {
public static void main(String[] args) {
Account acct = new Account(&quot;1234567&quot;, 0);
new DrawThread(&quot;取钱者&quot;, acct, 1000).start();
new DepositThread(&quot;存钱者甲&quot;, acct, 1000).start();
new DepositThread(&quot;存钱者乙&quot;, acct, 1000).start();
new DepositThread(&quot;存钱者丙&quot;, acct, 1000).start();
}
}
Java, 通信 |