当前位置:首页 > 嵌入式培训 > 嵌入式学习 > 讲师博文 > Java中线程的通信(一)

Java中线程的通信(一) 时间:2018-09-25      来源:未知

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中基于TCP通信(一)

下一篇:Java中线程的通信(二)

热点文章推荐
华清学员就业榜单
高薪学员经验分享
热点新闻推荐
前台专线:010-82525158 企业培训洽谈专线:010-82525379 院校合作洽谈专线:010-82525379 Copyright © 2004-2022 北京华清远见科技集团有限公司 版权所有 ,京ICP备16055225号-5京公海网安备11010802025203号

回到顶部