Home  >  Article  >  Java  >  How to implement a withdrawal applet with java multi-threading

How to implement a withdrawal applet with java multi-threading

王林
王林forward
2023-05-24 19:16:141361browse

1. Create java classes

How to implement a withdrawal applet with java multi-threading

Three java classes are created here. The first Account class is used to encapsulate each item in the account. kind of information; the third DrawThread class is used to implement the thread body; the second class mainly encapsulates the main function

2, Account class

public class Account {
    //封装账户编号,账户余额和两个成员变量
    private String accountNo;
    private double balance;

    public Account(){};
    public Account(String accountNo,double balance){
        this.accountNo=accountNo;
        this.balance=balance;
    }

    public void setAccountNo(String accountNo)
    {
        this.accountNo=accountNo;
    }
    public void setBalance(double balance){
        this.balance=balance;
    }

    public String getAccountNo(){
        return accountNo;
    }
    public double getBalance(){
        return balance;
    }

    public int hashCode(){
        return accountNo.hashCode();
    }

    public boolean equals(Object obj){
        if(this==obj){
            return true;
        }
        if (obj!=null&&obj.getClass()==Account.class){
            Account target=(Account)obj;
            return target.getAccountNo().equals(accountNo);
        }
        return false;
    }

}

3 , DrawThread class

public 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() {
        //使用account作为同步监视器,任何线程进入下面同步代码块之前
        //必须先获得对account账户的锁定——其他线程无法获得锁,也就无法修改它
        synchronized (account) {
            if (account.getBalance() >= drawAmount) {
                //吐出钞票
                System.out.println(getName() + "取钱成功!吐出钞票:" + drawAmount);
        /*
        try {
           Thread.sleep(1);
           }catch (InterruptedException ex)
        {
            ex.printStackTrace();
        }
        */
                //修改余额
                account.setBalance(account.getBalance() - drawAmount);
                System.out.println("\t余额为:" + account.getBalance());
            } else {
                System.out.println(getName() + "取钱失败!余额不足!");
            }
        }
    }
}

4, DrawTest class

public class DrawTest {
    public static void main(String[] args){
        //创建一个账户
        Account acct=new Account("1234567",1000);
        //模拟两个线程对同一个账户取钱
        new DrawThread("jack",acct,800).start();
        new DrawThread("rose",acct,800).start();
    }
}

Running result:

How to implement a withdrawal applet with java multi-threading

The above program One thing to note is the use of synchronized code blocks. It can solve the synchronization safety problem of the run() method. For example, when two threads send requests at the same time, an exception may occur.

Synchronization code block:

synchronized (obj){
//需要执行的代码
}

If we remove the run() method Continue to run the synchronization code block in

Account acct=new Account("1234567",1000);
new DrawThread("jack",acct,800).start();
new DrawThread("rose",acct,800).start();

(the bank account has a total of 1,000 yuan, Jack and rose respectively withdraw money from the same account)

Running result:

How to implement a withdrawal applet with java multi-threading

The above is the detailed content of How to implement a withdrawal applet with java multi-threading. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete