偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

漫畫:如何證明sleep不釋放鎖,而wait釋放鎖?

網(wǎng)絡(luò) 通信技術(shù)
本文我們通過(guò) synchronized 鎖定同一對(duì)象,來(lái)測(cè)試 wait 和 sleep 方法,再通過(guò)執(zhí)行結(jié)果的先后順序證明:wait 方法會(huì)釋放鎖,而 sleep 方法并不會(huì)。

本文轉(zhuǎn)載自微信公眾號(hào)「Java中文社群」,作者磊哥。轉(zhuǎn)載本文請(qǐng)聯(lián)系Java中文社群公眾號(hào)。 

  1. public class WaitDemo { 
  2.     private static Object locker = new Object(); 
  3.  
  4.     public static void main(String[] args) throws InterruptedException { 
  5.         WaitDemo waitDemo = new WaitDemo(); 
  6.  
  7.         // 啟動(dòng)新線程,防止主線程被休眠 
  8.         new Thread(() -> { 
  9.             try { 
  10.                 waitDemo.doWait(); 
  11.             } catch (InterruptedException e) { 
  12.                 e.printStackTrace(); 
  13.             } 
  14.         }).start(); 
  15.         Thread.sleep(200); // 此行本身沒有意義,是為了確保 wait() 先執(zhí)行再執(zhí)行 notify() 
  16.         waitDemo.doNotify(); 
  17.     } 
  18.  
  19.     /** 
  20.      * 執(zhí)行 wait() 
  21.      */ 
  22.     private void doWait() throws InterruptedException { 
  23.         synchronized (locker) { 
  24.             System.out.println("wait start."); 
  25.             locker.wait(); 
  26.             System.out.println("wait end."); 
  27.         } 
  28.     } 
  29.  
  30.     /** 
  31.      * 執(zhí)行 notify() 
  32.      */ 
  33.     private void doNotify() { 
  34.         synchronized (locker) { 
  35.             System.out.println("notify start."); 
  36.             locker.notify(); 
  37.             System.out.println("notify end."); 
  38.         } 
  39.     } 

以上程序的執(zhí)行結(jié)果為:

  • wait start.
  • notify start.
  • notify end.
  • wait end.

代碼解析

從上述代碼可以看出,我們給 wait() 和 notify() 兩個(gè)方法上了同一把鎖(locker),但在調(diào)用完 wait() 方法之后 locker 鎖就被釋放了,所以程序才能正常執(zhí)行 notify() 的代碼,因?yàn)槭峭话焰i,如果不釋放鎖的話,是不會(huì)執(zhí)行 notify()的代碼的,這一點(diǎn)也可以從打印的結(jié)果中證實(shí)(結(jié)果輸出順序),所以綜合以上情況來(lái)說(shuō) wait() 方法是釋放鎖的。

sleep 加鎖示例

  1. public class WaitDemo { 
  2.     private static Object locker = new Object(); 
  3.  
  4.     public static void main(String[] args) throws InterruptedException { 
  5.         WaitDemo waitDemo = new WaitDemo(); 
  6.         // 啟動(dòng)新線程,防止主線程被休眠 
  7.         new Thread(() -> { 
  8.             synchronized (locker) { 
  9.                 try { 
  10.                     System.out.println("sleep start."); 
  11.                     Thread.sleep(1000); 
  12.                     System.out.println("sleep end."); 
  13.                 } catch (InterruptedException e) { 
  14.                     e.printStackTrace(); 
  15.                 } 
  16.             } 
  17.         }).start(); 
  18.  
  19.         Thread.sleep(200); 
  20.         waitDemo.doNotify(); 
  21.     } 
  22.  
  23.     /** 
  24.      * 執(zhí)行 notify() 
  25.      */ 
  26.     private void doNotify() { 
  27.         synchronized (locker) { 
  28.             System.out.println("notify start."); 
  29.             locker.notify(); 
  30.             System.out.println("notify end."); 
  31.         } 
  32.     } 

以上程序的執(zhí)行結(jié)果為:

  • sleep start.
  • sleep end.
  • notify start.
  • notify end.

代碼解析

從上述代碼可以看出 sleep(1000) 方法(行號(hào):11)執(zhí)行之后,調(diào)用 notify() 方法并沒有獲取到 locker 鎖,從上述執(zhí)行結(jié)果中可以看出,而是執(zhí)行完 sleep(1000) 方法之后才執(zhí)行的 notify() 方法,因此可以證明調(diào)用 sleep() 方法并不會(huì)釋放鎖。

知識(shí)擴(kuò)展

1.sleep 和 wait 有什么區(qū)別?

sleep 和 wait 幾乎是所有面試中必問(wèn)的題,但想完全回答正確似乎沒那么簡(jiǎn)單。

對(duì)于 sleep 和 wait 的區(qū)別,通常的回答是這樣的:

  • wait 必須搭配 synchronize 一起使用,而 sleep 不需要;
  • 進(jìn)入 wait 狀態(tài)的線程能夠被 notify 和 notifyAll 線程喚醒,而 sleep 狀態(tài)的線程不能被 notify 方法喚醒;
  • wait 通常有條件地執(zhí)行,線程會(huì)一直處于 wait 狀態(tài),直到某個(gè)條件變?yōu)檎妫?sleep 僅僅讓你的線程進(jìn)入睡眠狀態(tài);
  • wait 方法會(huì)釋放對(duì)象鎖,但 sleep 方法不會(huì)。

但上面的回答顯然遺漏了一個(gè)重要的區(qū)別,在調(diào)用 wait 方法之后,線程會(huì)變?yōu)閃ATING 狀態(tài),而調(diào)用 sleep 方法之后,線程會(huì)變?yōu)? TIMED_WAITING 狀態(tài)。

2.wait 能不能在 static 方法中使用?為什么?

不能,因?yàn)?wait 方法是實(shí)例方法(非 static 方法),因此不能在 static 中使用,源碼如下:

  1. public final void wait() throws InterruptedException { 
  2.     wait(0); 

3.wait/notify 可以不搭配 synchronized 使用嗎?為什么?

不行,因?yàn)椴淮钆?synchronized 使用的話程序會(huì)報(bào)錯(cuò),如下圖所示:

 

更深層次的原因是因?yàn)椴患?synchronized 的話會(huì)造成 Lost Wake-Up Problem,喚醒丟失的問(wèn)題,詳情可見:https://juejin.im/post/5e6a4d8a6fb9a07cd80f36d1

 

總結(jié)

本文我們通過(guò) synchronized 鎖定同一對(duì)象,來(lái)測(cè)試 wait 和 sleep 方法,再通過(guò)執(zhí)行結(jié)果的先后順序證明:wait 方法會(huì)釋放鎖,而 sleep 方法并不會(huì)。同時(shí)我們還講了幾個(gè) wait 和 sleep 的常見面試問(wèn)題,希望本文可以幫助到你。

原文鏈接:原文鏈接:https://mp.weixin.qq.com/s/eYijPq_PtMd93dsMDUPNLQ

 

責(zé)任編輯:武曉燕 來(lái)源: Java中文社群
相關(guān)推薦

2022-08-02 08:00:49

sleepwait釋放鎖

2021-07-02 08:51:09

Redisson分布式鎖公平鎖

2021-10-01 00:12:12

Redis分布式

2024-03-13 13:25:09

Redis分布式鎖

2021-07-03 17:45:57

分布式Redisson MultiLock

2024-08-07 14:58:00

MySQL釋放鎖核心模塊

2021-07-09 06:48:31

ZooKeeperCurator源碼

2018-10-16 08:40:56

Linux鎖住鍵盤桌面應(yīng)用

2021-07-13 10:00:01

ThreadJoin方法

2014-08-14 10:38:30

SQL Server查詢

2024-09-03 08:06:30

AQS線程代碼

2024-03-07 07:37:03

AQS線程獨(dú)占鎖

2024-11-29 07:38:12

MySQL數(shù)據(jù)庫(kù)

2012-09-13 15:37:21

linux內(nèi)存

2025-04-23 08:45:00

悲觀鎖樂(lè)觀鎖并發(fā)控制機(jī)制

2024-09-03 15:14:42

2011-05-26 15:52:31

sleep()wait()

2024-10-15 09:27:36

2023-11-06 08:00:00

ReactJavaScript開發(fā)

2023-11-08 11:36:07

多云策略云計(jì)算
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)