syschronized去鎖對象底層monitor的原理

每個對象有一個監視器鎖(monitor)。當monitor被佔用時就會處於鎖定狀態,線程執行monitorenter指令時嘗試獲取monitor的所有權,過程如下: 1、如果monitor的進入數爲0,則該線程進入monitor,然後將進入數設置爲1,該線程即爲monitor的所有者。 2、如果線程已經佔有該monitor,只是重新進入,則進入monitor的進入數加1.重入鎖 3.如果其他線程已
相關文章
相關標籤/搜索