zoukankan      html  css  js  c++  java
  • java 同步代码块与同步方法

    同步代码块

    synchronized (obj) {
        // 代码块
    }

    obj 为同步监视器,以上代码的含义为:线程开始执行同步代码块(中的代码)之前,必须先获得对同步监视器的锁定。

    代码块中的代码是执行代码,即是某个方法中的某一部分代码,synchronized(obj){}只能出现在某个方法中。如:

        public void test() {
    
            synchronized (obj) {
                // 代码块
            }
        }

    而不能出现在其他位置,如下则报错:

    public class Test {
    
    
        public void test(String[] strs) {
    
        }
    
    // 报错,只能出现在某个方法中
    synchronized (obj) { } }

    同步代码块示例:

    定义一个Account类,用于存储账户金额

    class Account {
    
        // 账户余额
        private double balance;
    
        public Account(double balance) {
            this.balance = balance;
        }
    
        // 设置余额
        public void setBalance(double balance) {
            this.balance = balance;
        }
    
        // 取出余额
        public double getBalance() {
            return balance;
        }
    }

    定义1个线程类用于对某个账户进行操作(取出账户中的余额),该线程类不包含同步代码块

    class DrawMoney extends Thread {
    
        private Account account;  // 待取账户
        private double amount;    // 取出金额
    
        public DrawMoney(Account account, double amount) {
            this.account = account;
            this.amount = amount;
        }
    
        // 取出account中的余额,取出数量为amount
        public void run() {
            // 若account中的余额大于等于amount,取钱成功
            if (amount <= account.getBalance()) {
                System.out.println(getName() + " : " + "Get money:" + amount);
    
                // 线程休眠2毫秒
                try {
                    Thread.sleep(2);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                account.setBalance(account.getBalance() - amount);
                System.out.println(getName() + " : " + "Balance is " + account.getBalance());
            }
            else
                System.out.println("The balance is not enough");
        }
    }

    使用上述Account类及DrawMoney类定义一个账户,两个用户,两个用户同时对账户操作(取钱)。

    public class SynchronizedTest {
    
        public static void main(String[] args) {
            Account account = new Account(100);
            DrawMoney user1 = new DrawMoney(account, 70);
            DrawMoney user2 = new DrawMoney(account, 70);
            user1.start();
            user2.start();
        }
    }

    运行结果:

    Thread-0 : Get money:70.0
    Thread-1 : Get money:70.0
    Thread-0 : Balance is 30.0
    Thread-1 : Balance is -40.0

    由上可知,第二个用户取钱出现错误,余额不应当小于0。这是由于两个并发运行的线程(同时取钱的用户)同时对account操作,而不是一个取钱完成,再交给下一个。用户1还没来得及修改余额,用户2就开始取钱。

    修改上述线程类,同步其中的取钱操作

    class DrawCash extends Thread {
    
        private Account account;
        private double amount;
    
        public DrawCash(Account account, double amount) {
            this.account = account;
            this.amount = amount;
        }
    
        public void run() {
    
            // 使用account作为同步监视器,线程在执行下面的代码之前需要先锁定account
    
            synchronized (account) {
                if (amount <= account.getBalance()) {
                    System.out.println(getName() + " : " + "Get money:" + amount);
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    account.setBalance(account.getBalance() - amount);
                    System.out.println(getName() + " : " + "Balance is " + account.getBalance());
                }
                else
                    System.out.println(getName() + " : " + "The balance is not enough");
            }
        }
    }

    这时

    public class SynchronizedTest {
    
        public static void main(String[] args) {
            Account account = new Account(100);
            DrawCash user1 = new DrawCash(account, 70);
            DrawCash user2 = new DrawCash(account, 70);
            user1.start();
            user2.start();
        }
    }

    运行结果:

    Thread-0 : Get money:70.0
    Thread-0 : Balance is 30.0
    Thread-1 : The balance is not enough

    第一个线程执行同步代码块时锁定监视器account,第二个线程执行同步代码块时也需要锁定监视器account,

    但此时account被线程0锁定,故线程1只有在线程0的同步代码块执行完毕后才能执行其同步代码块。

    使用DrawMoney与DrawCash各定义一个用户,对同一个账户取钱。

    public class SynchronizedTest {
    
        public static void main(String[] args) {
            Account account = new Account(100);
            DrawCash user1 = new DrawCash(account, 70);
            DrawMoney user2 = new DrawMoney(account, 70);
            user1.start();
            user2.start();
        }
    }

    运行结果:

    Thread-0 : Get money:70.0
    Thread-1 : Get money:70.0
    Thread-0 : Balance is 30.0
    Thread-1 : Balance is -40.0

    结果依旧出错,这是由于线程0需要锁定监视器account,但线程1不需要,故该情况下account的访问仍会出现线程不安全。

    同步方法

     被synchronized修饰的方法为同步方法,同步方法的同步监视器为this,即与该方法对应的对象(该方法所在的类生成的对象)。

        public synchronized void draw() {
    
        }

    某个线程若要调用draw()方法,需要先锁定draw()对应的对象。

    修改Account类,添加同步方法

    class Account {
    
        // 账户余额
        private double balance;
    
        public Account(double balance) {
            this.balance = balance;
        }
    public synchronized void draw(double amount) {
            if (amount > balance)
                System.out.println(Thread.currentThread().getName() + " : " + "Balance is not enough");
            else {
                System.out.println(Thread.currentThread().getName() + " : " + amount);
                balance -= amount;
                System.out.println(Thread.currentThread().getName() + " : " + "Balance is " + balance);
            }
        }
    
    }

    修改DrawMoney类

    class DrawMoney extends Thread {
    
        private Account account;  // 待取账户
        private double amount;    // 取出金额
    
        public DrawMoney(Account account, double amount) {
            this.account = account;
            this.amount = amount;
        }
    
        // 取出account中的余额,取出数量为amount
        public void run() {
            account.draw(amount);
        }
    }

    这时

    public class SynchronizedTest {
    
        public static void main(String[] args) {
            Account account = new Account(100);
            DrawMoney user1 = new DrawMoney(account, 70);
            DrawMoney user2 = new DrawMoney(account, 70);
            user1.start();
            user2.start();
        }
    }

    运行结果:

    Thread-0 : 70.0
    Thread-0 : Balance is 30.0
    Thread-1 : Balance is not enough

    可见,线程是安全的

    线程0调用draw()方法时锁定监视器account,1线程调用draw()时也需要锁定监视器account,

    但此时account被线程0锁定,故线程1只有在线程0的调用完毕后才能调用。

    上述的同步方法也可以用同步代码块实现:

    class Account {
    
        // 账户余额
        private double balance;
    
        public Account(double balance) {
            this.balance = balance;
        }
    
        public void draw(double amount) {
    
            synchronized (this) {
                if (amount > balance)
                    System.out.println(Thread.currentThread().getName() + " : " + "Balance is not enough");
                else {
                    System.out.println(Thread.currentThread().getName() + " : " + amount);
                    balance -= amount;
                    System.out.println(Thread.currentThread().getName() + " : " + "Balance is " + balance);
                }
            }
        }
    }

    总结:

    同步代码块与同步方法都是表明在执行某段代码前需要先锁定某个对象,同步代码块需指定,同步方法默认为this。

  • 相关阅读:
    Python进制转换
    Python matplotlib笔记
    Python Numpy,Pandas基础笔记
    Android调用WebService
    逻辑回归 Logistic Regression
    奇异值分解 SVD
    Laravel 队列不执行的原因,job缓存
    Vim使用技巧(0) -- 博主的vim配置
    Vim使用技巧(5) -- 宏的录制与使用
    linux crontab 鉴定令牌不再有效,需要新的鉴定令牌 [ You (root) are not allowed to access to (crontab) because of pam configuration.]
  • 原文地址:https://www.cnblogs.com/deltadeblog/p/9226995.html
Copyright © 2011-2022 走看看