• 多线程编程


       只需要耽搁您2分钟时间就可以解决缠绕我心中的迷雾,往这里看。

           最近在研究多线程编程,有一些问题把我给绕住了,望能给予指点,不胜感激。

    最基本的一个:模仿取钱操作

    public class Account {
    private
    String accountNum; private double balance; public Account() { } public Account(String accountnum, double balance) { this.accountNum = accountnum; this.balance = balance; } public void setAccountNum(String accountNum) { this.accountNum = accountNum; } public void setBalance(double balance) { this.balance = balance; } public String getAccountNum() { return accountNum; } public double getBalance() { return balance; } @Override public int hashCode() { return this.accountNum.hashCode(); } @Override public boolean equals(Object obj) { if(this==obj){ return true; } if(obj!=null&&obj.getClass()==Account.class){ Account target = (Account)obj; return target.getAccountNum().equals(this.getAccountNum()); } return false; }
    public class DrawMoney implements Runnable{
        private Account account;
        private double drawNum;
        public DrawMoney(String name,Account account,double drawnum){
            this.account = account;
            this.drawNum = drawnum;
        }
        public void setAccount(Account account) {
            this.account = account;
        }
        public Account getAccount() {
            return account;
        }
        public void setDrawNum(double drawNum) {
            this.drawNum = drawNum;
        }
    
        public double getDrawNum() {
            return drawNum;
        }
        @Override
        public void run() {
            synchronized (account) {
                String name = Thread.currentThread().getName();
            if(drawNum<=this.account.getBalance()){
                System.out.println(name+"取钱成功!"+this.drawNum);
                this.account.setBalance(this.account.getBalance()-this.drawNum);
                System.out.println(name+"余额为2:"+this.account.getBalance());
            }
            else{
                System.out.println("余额不足,无法完成取钱");
            }
            }
        }
        public static void main(String[] args) {
            Account account =new Account("zhangsan", 1000);
            DrawMoney dw1 = new DrawMoney("zhangsan", account, 800);
            DrawMoney dw2 = new DrawMoney("lisi", account, 800);
    //        第一种模拟方式
            Thread t1 = new Thread(dw1);t1.setName("zhangsan");
            Thread t2 = new Thread(dw1);t2.setName("lisi");
    //        t1.start();
    //        t2.start();
    //        第二种模拟方式
            Thread tt1 = new Thread(dw1);tt1.setName("zhangsan");
            Thread tt2 = new Thread(dw2);tt2.setName("lisi");
            tt1.start();
            tt2.start();
        }
        
    }

    问题:我想知道第一种和第二种模拟方式相同吗?

    第一种模拟方式相当于只new了一个DrawMoney,然后给出两个Thread线程对他进行操作。此时的synchronized里面的锁必须是account,

    第二种模拟方式相当于new了两个DrawMoney,但是这两个DrawMoney是针对同一账户的,然后给出两个Thread,此时的synchronized里面的锁可以是account和this

    问这两种方式有区别吗?望指点.小弟多谢.

    
    
    
  • 相关阅读:
    Hashset;HashMap;ArrayList;
    排序方法(冒泡排序,直接选择排序,反转排序);
    父类引用指向子类的对象;
    SQL分支语句与循环语句
    ORCAL 数据库的约束以及SQL语言的四种类型
    Oracle基础数据类型与运算符
    集合
    数组的几种排序
    转换日期格式的工具类
    equals方法重写
  • 原文地址:https://www.cnblogs.com/lovebaoqiang/p/Thread.html
Copyright © 2020-2023  润新知