死锁 java实现

public class Test {
    public static Object deadLock1 = new Object();
    public static Object deadLock2 = new Object();
    public static void main(String[] args) throws Exception{
        Bank bank = new Bank("s001", 1000);
        // 创建线程对象
        SaveAccount sAccount = new SaveAccount(bank);
        DrawAccount dAccount = new DrawAccount(bank);
        Thread save = new Thread(sAccount);
        Thread draw = new Thread(dAccount);
        save.start();       // +100
//        save.join();
        draw.start();       // -200
//        save.join();      // join(): 阻塞别的线程,直到线程save结束
//        draw.join();
        System.out.println(bank);
    }
}

public class DrawAccount implements Runnable{
    Bank bank;
    public DrawAccount(Bank bank) {
        this.bank = bank;
    }

    @Override
    public void run() {
        int i = 0;
        while (i < 5) {
            try {
                bank.drawAccount();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            i++;
        }
    }
}

public class SaveAccount implements Runnable {
    Bank bank;
    public SaveAccount(Bank bank) {
        this.bank = bank;
    }

    @Override
    public void run() {
        int i = 0;
        while (i < 5) {
            try {
                bank.saveAccount();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            i++;
        }
    }
}

public class Bank {
    private String account;// 账户
    private int balance;// 余额
    boolean flag = true;//控制线程执行顺序

    public Bank(String account, int balance) {
        this.setAccount(account);
        this.setBalance(balance);
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "银行 [ 账号:" + account + ", 余额:" + balance + "]";
    }

//    public synchronized void saveAccount() throws InterruptedException {
    public  void saveAccount() throws InterruptedException {
//        if (!flag) {
//            wait();
//        }
        System.out.println("save start");
        synchronized (Test.deadLock1) {
            Thread.sleep(1000);
            synchronized (Test.deadLock2) {
                System.out.println("dead lock save");
            }
        }

        // 获取当前账户的余额
        int balance = getBalance();
        // 修改余额,存100
        balance = balance + 100;
        setBalance(balance);
        System.out.println("存款后的账户余额:" + balance);
//        flag = false;
//        notifyAll();
    }

    public  void drawAccount() throws InterruptedException {
//        if (flag) {
//            wait();
//        }

        System.out.println("draw start");
        synchronized (Test.deadLock2) {
            Thread.sleep(1000);
            synchronized (Test.deadLock1) {
                System.out.println("dead lock draw");
            }
        }

        // 获得当前的账户余额
        int balance = getBalance();
        // 修改余额,取200
        balance = balance - 200;
        // 修改账户余额
        setBalance(balance);
        System.out.println("取款后的账户余额:" + balance);
//        flag = true;
//        notifyAll();
    }
}

猜你喜欢

转载自blog.csdn.net/quitozang/article/details/81838879
今日推荐