執行緒同步方法及同步塊
阿新 • • 發佈:2020-07-16
執行緒同步方法及同步塊
同步方法
- 由於我們可以通過private關鍵字來保證資料物件只能被方法訪問,所以我們只需要針對方法提出一套機制,這套機制就是synchronized關鍵字,它包括兩種用法:
- synchronized方法和synchronized塊.
- 同步方法:public synchronized void method(int args)[]
- synchronized方法控制對"物件"的訪問,每一個物件對應一把鎖,每個synchronized方法都必須獲得呼叫該方法的物件的鎖才能執行,否則執行緒會阻塞,方法一旦執行,就獨佔該鎖,直到該方法返回才釋放鎖,後面被阻塞的執行緒才能獲得這個鎖,繼續執行
- 缺陷:若將一個大的方法申明為synchronized將會影響效率
同步塊
- 同步塊:synchronized(Obj){}
- Obj稱之為同步監視器
- Obj可以是任何物件,但是推薦使用共享資源作為同步監視器
- 同步方法中無需指定同步監視器,因為同步方法的同步監視器就是this,就是這個物件本身,或者是class[反射中講解]
- 同步監視器的執行過程
- 第一個執行緒訪問,鎖定同步監視器,執行其中程式碼
- 第二個執行緒訪問,發現同步監視器被鎖定,無法訪問
- 第一個執行緒訪問完畢,解鎖同步監視器
- 第二個執行緒訪問,發現同步監視器沒有鎖,然後鎖定並訪問
三大不安全案例改進
買票
//不安全的買票 //執行緒不安全,有負數 public class UnsafeBuyTicket { public static void main(String[] args) { BuyTicket station = new BuyTicket(); new Thread(station,"苦逼的我").start(); new Thread(station,"牛逼的你們").start(); new Thread(station,"可惡的黃牛黨").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 { //判斷是否有票 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(1000,"結婚基金"); Drawing you = new Drawing(account,50,"你"); Drawing girlFriend = new Drawing(account,100,"girlFriend"); you.start(); girlFriend.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; } //取錢 //synchronized預設鎖的是this.(本身) @Override public void run() { //鎖的物件就是變化的量,需要增刪改查 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); //getName()+Thread.currentThread().getName() = this.getName() System.out.println(this.getName()+"手裡的錢:"+nowMoney); } } }
不安全的集合
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 < 100000; i++) {
new Thread(()->{
synchronized (list){
list.add(Thread.currentThread().getName());
}
}).start();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(list.size());
}
}