线程同步之模拟银行取钱实例

  线程同步的机制就是队列和锁,把共享的资源加上一把锁,然后把线程进行排队,实现安全的线程。下面为大家模拟银行取钱的实例,代码如下:

public class Unsafebank {
    public static void main(String[] args) {
        Account account = new Account(100, "农业银行");
        //创建两个线程
        Bank youself = new Bank("自己", account, 50);
        Bank wife = new Bank("你的媳妇", account, 100);
        youself.start();
        wife.start();
    }
}
//账户
class Account {
    int money;
    String name;

    public Account(int money, String name) {
        this.money = money;  // 账户余额
        this.name = name;    //账户名称
    }
}

//银行
class Bank extends Thread {
    Account account;
    int nowMoney;  //手里剩余的金钱
    int withdrawMoney; //取钱金额

    public Bank(String name, Account account, int withdrawMoney) {
        super(name);
        this.account = account;
        this.withdrawMoney = withdrawMoney;
    }

    public void run() {
        //判断能否取钱
        withdrawMoney();
    }

    private void withdrawMoney() {
        if (account.money - withdrawMoney < 0) {
            return;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        account.money = account.money - withdrawMoney;
        nowMoney = nowMoney + withdrawMoney;
        System.out.println(this.account.name + "账户余额为:" + account.money);
        System.out.println(this.getName() + "手里的钱:" + nowMoney);
    }
}

  上面的代码是创建了两个线程对象对账户的余额进行操作,运行的结果为:

线程同步之模拟银行取钱实例_第1张图片
  由上面的结果可以看出,这个线程的不安全的,那么这个时候我们就需要用到进程同步来使这个线程安全。因为这这两个线程对象的操作同一个账户,所以这个账户需要被加一把锁,那我们可以用同步块来实现,那么我们就在取钱的时候,把账户加上一把锁,使线程安全(哪个对象不安全就锁哪个对象)。代码如下:

public class Unsafebank {
    public static void main(String[] args) {
        Account account = new Account(100, "农业银行");
        //创建两个线程
        Bank youself = new Bank("自己", account, 50);
        Bank wife = new Bank("你的媳妇", account, 100);
        youself.start();
        wife.start();
    }
}
//账户
class Account {
    int money;
    String name;

    public Account(int money, String name) {
        this.money = money;  // 账户余额
        this.name = name;    //账户名称
    }
}

//银行
class Bank extends Thread {
    Account account;
    int nowMoney;  //手里剩余的金钱
    int withdrawMoney; //取钱金额

    public Bank(String name, Account account, int withdrawMoney) {
        super(name);
        this.account = account;
        this.withdrawMoney = withdrawMoney;
    }

    public void run() {
        //判断能否取钱
        withdrawMoney();
    }

    private void withdrawMoney() {
        //给账户加上一把锁
        synchronized (account){
            if (account.money - withdrawMoney < 0) {
                System.out.println("抱歉,余额不足");
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            account.money = account.money - withdrawMoney;
            nowMoney = nowMoney + withdrawMoney;
            System.out.println(this.account.name + "账户余额为:" + account.money);
            System.out.println(this.getName() + "手里的钱:" + nowMoney);
        }

    }
}

  此时运行结果为:

线程同步之模拟银行取钱实例_第2张图片
  由这个结果看出,此时线程已经安全。

你可能感兴趣的:(线程同步之模拟银行取钱实例)