资源简介

模拟实现多线程处理银行的实时转账交易,代码完整,可以完美运行~

资源截图

代码片段和文件信息

package task12_23.UpgradeTransferAccount;

/**
 * Created by QXY on 2018/12/14.
 */
public class Account {
    private String id;
    private float balance;
    private int transanctionNum=0;

    private boolean available=true;

    public String getId() {
        return id;
    }

    public float getBalance() {
        return balance;
    }

    public int getTransanctionNum() {
        return transanctionNum;
    }

    Account(String  idfloat balance){
        this.id = id;
        this.balance = balance;
    }

    public synchronized void checkavaialbe(){

        while (!available){
//            System.out.println(this.getId()+“ do not prepare well!***“+Thread.currentThread().getName()+“:Please wait~“);
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
//        System.out.println(Thread.currentThread().getName()+“:I get the account:“+this.getId());
        this.available = false;
    }

    public synchronized boolean checkavaialbe(int locknum){

        if(locknum ==0) {
            while (!available) {
//                System.out.println(this.getId() + “ do not prepare well!***“ + Thread.currentThread().getName() + “:Please wait~“);
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
//            System.out.println(Thread.currentThread().getName() + “:I get the account:“ + this.getId());
            this.available = false;
            return true;
        }else {
            if(!available) {
//                System.out.println(Thread.currentThread().getName() + “ failed to lock the account:“ + this.getId() + “:I will quit~“);
                return false;
            }else {
//                System.out.println(Thread.currentThread().getName() + “:I get the account:“ + this.getId());
                this.available = false;
                return true;
            }
        }
    }

    public synchronized void balanceadd(float amount)
    {
        this.balance += amount;
    }

    public synchronized boolean balancesub(float amount)
    {
        if(amount > this.balance)
            return false;
        this.balance -= amount;
        return true;
    }

    public synchronized void setAvailable(){
        available=true;
//        System.out.println(Thread.currentThread().getName()+“:I‘m over~return the account:“+this.getId());
        notifyAll();
    }

    public synchronized void transanctionNumAdd()
    {
        this.transanctionNum ++;
    }


}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        2733  2018-12-15 11:09  UpgradeTransferAccount\Account.java
     文件        2873  2018-12-15 11:30  UpgradeTransferAccount\MainBank.java
     文件         341  2018-12-14 23:30  UpgradeTransferAccount\Transaction.java
     文件     3647799  2018-12-15 11:25  UpgradeTransferAccount\Transaction_record.txt
     文件        2885  2018-12-15 11:21  UpgradeTransferAccount\TransferThread.java
     文件         864  2018-12-15 11:24  UpgradeTransferAccount\utils.java
     文件       15068  2018-12-04 16:07  UpgradeTransferAccount\多线程作业.docx
     目录           0  2018-12-15 11:43  UpgradeTransferAccount\

评论

共有 条评论