同步方法及同步塊

ℒ ℬ發表於2020-10-10

同步方法

  1. 由於我們可以通過private關鍵字來保證資料物件只能被方法訪問,所以我們只需要針對方法提出一套機制,這套機制就是synchronized關鍵字,包括兩種用法:synchronized方法synchronized塊

    同步方法:public synchronized void method(int args) {}
    
  2. synchronized方法控制對“物件”的訪問每個物件對應一把鎖,每個synchronized方法都必須獲得呼叫該方法的物件的鎖才能執行,否則執行緒會阻塞,方法一旦執行,就獨佔該鎖,直到該方法返回才釋放鎖,後面被阻塞的執行緒才能獲得這個鎖,繼續執行。

缺陷:若將一個大的方法申明為synchronized將會影響效率

  1. 方法裡面需要修改的內容才需要鎖,鎖的太多,浪費資源。

同步塊

  1. 同步塊(格式):synchronized(Obj) {}
  2. Obj稱之為同步監視器:監視的物件就是變化的量,需要增刪改的物件
    • Obj可以是任何物件,但是推薦使用共享資源作為同步監視器
    • 同步方法中無需指定同步監視器,因為同步方法的同步監視器就是this,就是這個物件的本身,或者是class
  3. 同步監視器的執行過程
    • 第一個執行緒訪問,鎖定同步監視器,執行其中程式碼
    • 第二個執行緒訪問,發現同步監視器被鎖定,無法訪問
    • 第一個執行緒訪問完畢,解鎖同步監視器
    • 第二個執行緒訪問,發現同步監視器沒有鎖,然後鎖定並訪問

使用synchronized後的三大不安全案例

案例一
//買票
//執行緒不安全:可能會出現負的,使用synchronized後解決
public class UnsafeBuyTicket {

    public static void main(String[] args) {
        BuyTicket station = new BuyTicket();

        new Thread(station, "x").start();
        new Thread(station, "y").start();
        new Thread(station, "z").start();
    }
}

class BuyTicket implements Runnable{

    //票
    private int ticketNums = 10;
    boolean flag = true; //外部停止方法

    @Override
    public void run() {
        //買票
        while (flag){
            try {
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //買票的方法
    //synchronized 同步方法,鎖的是this--本身
    private synchronized void buy() throws InterruptedException {  //使用synchronized 
        //判斷是否有票
        if (ticketNums <= 0){
            flag = false;
            return;
        }
        //模擬延時
        Thread.sleep(100);
        //買票
        System.out.println(Thread.currentThread().getName() + "拿到" + ticketNums--);
    }
}
案例二
//取錢:兩人去銀行取錢,賬戶
public class UnsafeBank {

    public static void main(String[] args) {
        //賬戶
        Account account = new Account(100, "創業基金");

        Drawing you = new Drawing(account, 50, "你");
        Drawing girFriend = new Drawing(account, 100, "girFriend");

        you.start();
        girFriend.start();

    }
}

//賬戶
class Account{
    int money; //餘額
    String name; //卡名

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }
}

//銀行:模擬取款
class Drawing extends Thread{
    Account account; //賬戶
    //取了多少錢
    int drawingMoney;
    //手裡有多少錢
    int nowMoney;

    public Drawing(Account account, int drawingMoney, String name) {
        super(name);
        this.account = account;
        this.drawingMoney = drawingMoney;
    }

    //取錢
    @Override
    public void run() {

        //synchronized塊
        //鎖的物件就是變化的量,需要增刪改的物件
        synchronized (account){
            //判斷有沒有錢
            if (account.money - drawingMoney < 0) {
                System.out.println(Thread.currentThread().getName() + "錢不夠不能取");
                return;
            }

            //模擬延時:sleep可以放大問題的發生性
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //卡內餘額 = 餘額 - 你取的錢
            account.money = account.money - drawingMoney;
            //手裡的錢
            nowMoney = nowMoney + drawingMoney;

            System.out.println(account.name + "餘額為:" + account.money); //賬戶餘額
            //Thread.currentThread().getName() = this.getName()
            System.out.println(this.getName() + "手裡的錢:" +nowMoney); //手裡的錢
        }

    }
}
案例三
import javax.swing.plaf.nimbus.State;
import java.util.ArrayList;
import java.util.List;

//執行緒不安全的集合
public class UnsafeList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                synchronized (list) {
                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }

        //模擬延時
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }
}

相關文章