打印

Java中线程的通信

[复制链接]
194|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
电员师|  楼主 | 2018-8-22 11:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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("1234567", 0);




new DrawThread("取钱者", acct, 1000).start();




new DepositThread("存钱者甲", acct, 1000).start();




new DepositThread("存钱者乙", acct, 1000).start();




new DepositThread("存钱者丙", acct, 1000).start();




}




}










Java, 通信

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

471

主题

480

帖子

0

粉丝