java------线程通信实例

本文提供了一个使用Java实现的银行账户存款与取款的线程同步示例。通过一个Account类来模拟真实世界中银行账户的操作,包括存取款功能,并通过synchronized关键字确保线程安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

package lyfpractice.src.com.comunication;

/**
 * Created by fangjiejie on 2016/12/21.
 */
public class Account {
    String name;
    double money;

    public Account(String name, double money) {
        this.name = name;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
    boolean flag=false;//可存不可取,true可取不可存
    void savemoney(double money){//存钱
        synchronized (this){
            if(flag==true){
                try {
                    System.out.print(Thread.currentThread().getName());
                    System.out.println("现在不能存!");
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                System.out.print(Thread.currentThread().getName());
                    this.setMoney(this.getMoney() + money);
                    System.out.println("已经存"+money+"元");

                }
            flag=true;
            notifyAll();
            }
        }
    void takemoney(double money){//取钱
        synchronized (this){
            if(flag==false){
                try {
                    System.out.print(Thread.currentThread().getName());
                    System.out.println("现在不能取!");
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                if(this.getMoney()<money){
                    System.out.print(Thread.currentThread().getName());
                    System.out.println("余额不足!");
                }
                else {
                    System.out.print(Thread.currentThread().getName());
                    System.out.println("已经取到"+money+"元");
                    this.setMoney(this.getMoney() - money);
                }
            }
            flag=false;
            notifyAll();

        }
    }

}
package lyfpractice.src.com.comunication;

/**
 * Created by fangjiejie on 2016/12/21.
 */
public class Save extends Thread{
    String people;
    Account account;
    double m;
    public Save(String people,double m, Account account) {
        this.people = people;
        this.m=m;
        this.account = account;
    }

    @Override
    public void run() {
        account.savemoney(m);
    }
}
package lyfpractice.src.com.comunication;

/**
 * Created by fangjiejie on 2016/12/21.
 */
public class Take extends Thread{
    String people;
    Account account;
    double m;
    public Take(String people,double m, Account account) {
        this.people = people;
        this.m=m;
        this.account = account;
    }

    @Override
    public void run() {

        account.takemoney(m);
    }
}
package lyfpractice.src.com.comunication;

/**
 * Created by fangjiejie on 2016/12/21.
 */
public class test {
    public static void main(String[] args) {
        Account l=new Account("lyf",0);
        new Save("mm",100,l).start();
        Save a=new Save("dd",100,l);
        a.start();
        new Save("gg",100,l).start();
        new Take("dd",100,l).start();
        new Save("gg",100,l).start();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值