{"id":8006,"date":"2024-06-27T10:01:01","date_gmt":"2024-06-27T02:01:01","guid":{"rendered":""},"modified":"2024-06-27T10:01:01","modified_gmt":"2024-06-27T02:01:01","slug":"synchronized\u9501\u539f\u7406_reentrantlock\u5e95\u5c42\u539f\u7406","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8006.html","title":{"rendered":"synchronized\u9501\u539f\u7406_reentrantlock\u5e95\u5c42\u539f\u7406"},"content":{"rendered":"

\u5e95\u5c42\u539f\u7406\u5b9e\u73b0\u7684\u4e0d\u540c<\/span><\/span><\/p>\n<\/p>\n

\u4e92\u65a5\u540c\u6b65\u6700\u4e3b\u8981\u7684\u95ee\u9898\u5c31\u662f\u8fdb\u884c\u7ebf\u7a0b\u963b\u585e\u548c\u5524\u9192\u6240\u5e26\u6765\u7684\u6027\u80fd\u95ee\u9898\uff0c\u56e0\u800c\u8fd9\u79cd\u540c\u6b65\u53c8\u79f0\u4e3a\u963b\u585e\u540c\u6b65\uff0c\u5b83\u5c5e\u4e8e\u4e00\u79cd\u60b2\u89c2\u7684\u5e76\u53d1\u7b56\u7565\uff0c\u5373\u7ebf\u7a0b\u83b7\u5f97\u7684\u662f\u72ec\u5360\u9501\u3002<\/span>\u72ec\u5360\u9501\u610f\u5473\u7740\u5176\u4ed6\u7ebf\u7a0b\u53ea\u80fd\u4f9d\u9760\u963b\u585e\u6765\u7b49\u5f85\u7ebf\u7a0b\u91ca\u653e\u9501<\/span>\u3002\u800c\u5728 CPU \u8f6c\u6362\u7ebf\u7a0b\u963b\u585e\u65f6\u4f1a\u5f15\u8d77\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u5f53\u6709\u5f88\u591a\u7ebf\u7a0b\u7ade\u4e89\u9501\u7684\u65f6\u5019\uff0c\u4f1a\u5f15\u8d77 CPU \u9891\u7e41\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5bfc\u81f4\u6548\u7387\u5f88\u4f4e\u3002synchronized \u91c7\u7528\u7684\u4fbf\u662f\u8fd9\u79cd\u5e76\u53d1\u7b56\u7565\u3002<\/span><\/p>\n

\u968f\u7740\u6307\u4ee4\u96c6\u7684\u53d1\u5c55\uff0c\u6211\u4eec\u6709\u4e86\u53e6\u4e00\u79cd\u9009\u62e9\uff1a<\/span>\u57fa\u4e8e\u51b2\u7a81\u68c0\u6d4b\u7684\u4e50\u89c2\u5e76\u53d1\u7b56\u7565<\/span>\uff0c\u901a\u4fd7\u5730\u8bb2\u5c31\u662f\u5148\u8fdb\u6027\u64cd\u4f5c\uff0c\u5982\u679c\u6ca1\u6709\u5176\u4ed6\u7ebf\u7a0b\u4e89\u7528\u5171\u4eab\u6570\u636e\uff0c\u90a3\u64cd\u4f5c\u5c31\u6210\u529f\u4e86\uff0c\u5982\u679c\u5171\u4eab\u6570\u636e\u88ab\u4e89\u7528\uff0c\u4ea7\u751f\u4e86\u51b2\u7a81\uff0c\u90a3\u5c31\u518d\u8fdb\u884c\u5176\u4ed6\u7684\u8865\u507f\u63aa\u65bd\uff08\u6700\u5e38\u89c1\u7684\u8865\u507f\u63aa\u65bd\u5c31\u662f\u4e0d\u65ad\u5730\u91cd\u62fe\uff0c\u76f4\u5230\u8bd5\u6210\u529f\u4e3a\u6b62\uff09\uff0c\u8fd9\u79cd\u4e50\u89c2\u7684\u5e76\u53d1\u7b56\u7565\u7684\u8bb8\u591a\u5b9e\u73b0\u90fd\u4e0d\u9700\u8981\u628a\u7ebf\u7a0b\u6302\u8d77\uff0c\u56e0\u6b64\u8fd9\u79cd\u540c\u6b65\u88ab\u79f0\u4e3a\u975e\u963b\u585e\u540c\u6b65\u3002ReetrantLock \u91c7\u7528\u7684\u4fbf\u662f\u8fd9\u79cd\u5e76\u53d1\u7b56\u7565\u3002<\/span><\/p>\n

\u5728\u4e50\u89c2\u7684\u5e76\u53d1\u7b56\u7565\u4e2d\uff0c\u9700\u8981\u64cd\u4f5c\u548c\u51b2\u7a81\u68c0\u6d4b\u8fd9\u4e24\u4e2a\u6b65\u9aa4\u5177\u5907\u539f\u5b50\u6027\uff0c\u5b83\u9760\u786c\u4ef6\u6307\u4ee4\u6765\u4fdd\u8bc1\uff0c\u8fd9\u91cc\u7528\u7684\u662f CAS \u64cd\u4f5c\uff08Compare and Swap\uff09\u3002JDK1.5 \u4e4b\u540e\uff0cJava \u7a0b\u5e8f\u624d\u53ef\u4ee5\u4f7f\u7528CAS\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7814\u7a76 ReentrantLock \u7684\u6e90\u4ee3\u7801\uff0c\u4f1a\u53d1\u73b0\u5176\u4e2d\u6bd4\u8f83\u91cd\u8981\u7684\u83b7\u5f97\u9501\u7684\u4e00\u4e2a\u65b9\u6cd5\u662f compareAndSetState\uff0c\u8fd9\u91cc\u5176\u5b9e\u5c31\u662f\u8c03\u7528\u7684 CPU \u63d0\u4f9b\u7684\u7279\u6b8a\u6307\u4ee4\u3002\u73b0\u4ee3\u7684 CPU \u63d0\u4f9b\u4e86\u6307\u4ee4\uff0c\u53ef\u4ee5\u81ea\u52a8\u66f4\u65b0\u5171\u4eab\u6570\u636e\uff0c\u800c\u4e14\u80fd\u591f\u68c0\u6d4b\u5230\u5176\u4ed6\u7ebf\u7a0b\u7684\u5e72\u6270\uff0c\u800c compareAndSet() \u5c31\u7528\u8fd9\u4e9b\u4ee3\u66ff\u4e86\u9501\u5b9a\u3002\u8fd9\u4e2a\u7b97\u6cd5\u79f0\u4f5c\u975e\u963b\u585e\u7b97\u6cd5\uff0c\u610f\u601d\u662f\u4e00\u4e2a\u7ebf\u7a0b\u7684\u5931\u8d25\u6216\u8005\u6302\u8d77\u4e0d\u5e94\u8be5\u5f71\u54cd\u5176\u4ed6\u7ebf\u7a0b\u7684\u5931\u8d25\u6216\u6302\u8d77\u3002<\/p>\n

Java 5 \u4e2d\u5f15\u5165\u4e86\u6ce8\u5165 AutomicInteger\u3001AutomicLong\u3001AutomicReference \u7b49\u7279\u6b8a\u7684\u539f\u5b50\u6027\u53d8\u91cf\u7c7b\uff0c\u5b83\u4eec\u63d0\u4f9b\u7684\u5982\uff1acompareAndSet()\u3001incrementAndSet()\u548cgetAndIncrement()\u7b49\u65b9\u6cd5\u90fd\u4f7f\u7528\u4e86 CAS \u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u5b83\u4eec\u90fd\u662f\u7531\u786c\u4ef6\u6307\u4ee4\u6765\u4fdd\u8bc1\u7684\u539f\u5b50\u65b9\u6cd5\u3002<\/p>\n

<\/p>\n

\u7528\u9014\u6bd4\u8f83<\/h4>\n<\/p>\n

\u57fa\u672c\u8bed\u6cd5\u4e0a\uff0cReentrantLock \u4e0e synchronized \u5f88\u76f8\u4f3c\uff0c\u5b83\u4eec\u90fd\u5177\u5907\u4e00\u6837\u7684\u7ebf\u7a0b\u91cd\u5165\u7279\u6027\uff0c\u53ea\u662f\u4ee3\u7801\u5199\u6cd5\u4e0a\u6709\u70b9\u533a\u522b\u800c\u5df2\uff0c\u4e00\u4e2a\u8868\u73b0\u4e3a API \u5c42\u9762\u7684\u4e92\u65a5\u9501\uff08Lock\uff09\uff0c\u4e00\u4e2a\u8868\u73b0\u4e3a\u539f\u751f\u8bed\u6cd5\u5c42\u9762\u7684\u4e92\u65a5\u9501\uff08synchronized\uff09\u3002ReentrantLock \u76f8\u5bf9 synchronized \u800c\u8a00\u8fd8\u662f\u589e\u52a0\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u4e09\u9879\uff1a<\/p>\n