{"id":536,"date":"2023-09-16T16:18:01","date_gmt":"2023-09-16T08:18:01","guid":{"rendered":""},"modified":"2023-09-17T00:41:54","modified_gmt":"2023-09-16T16:41:54","slug":"java-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%90%8c%e6%ad%a5%e7%9a%84%e4%ba%94%e7%a7%8d%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/mushiming.com\/536.html","title":{"rendered":"Java \u591a\u7ebf\u7a0b\u540c\u6b65\u7684\u4e94\u79cd\u65b9\u6cd5"},"content":{"rendered":"
\u4e00\u3001\u5f15\u8a00<\/strong><\/p>\n \u524d\u51e0\u5929\u9762\u8bd5\uff0c\u88ab\u5927\u5e08\u8650\u6b8b\u4e86\uff0c\u597d\u591a\u57fa\u7840\u77e5\u8bc6\u5fc5\u987b\u5f97\u91cd\u65b0\u62ff\u8d77\u6765\u554a\u3002\u95f2\u8bdd\u4e0d\u591a\u8bf4\uff0c\u8fdb\u5165\u6b63\u9898\u3002<\/p>\n \n \u4e8c\u3001\u4e3a\u4ec0\u4e48\u8981\u7ebf\u7a0b\u540c\u6b65<\/strong><\/p>\n \u56e0\u4e3a\u5f53\u6211\u4eec\u6709\u591a\u4e2a\u7ebf\u7a0b\u8981\u540c\u65f6\u8bbf\u95ee\u4e00\u4e2a\u53d8\u91cf\u6216\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u8fd9\u4e9b\u7ebf\u7a0b\u4e2d\u65e2\u6709\u8bfb\u53c8\u6709\u5199\u64cd\u4f5c\u65f6\uff0c\u5c31\u4f1a\u5bfc\u81f4\u53d8\u91cf\u503c\u6216\u5bf9\u8c61\u7684\u72b6\u6001\u51fa\u73b0\u6df7\u4e71\uff0c\u4ece\u800c\u5bfc\u81f4\u7a0b\u5e8f\u5f02\u5e38\u3002\u4e3e \u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u4e00\u4e2a\u94f6\u884c\u8d26\u6237\u540c\u65f6\u88ab\u4e24\u4e2a\u7ebf\u7a0b\u64cd\u4f5c\uff0c\u4e00\u4e2a\u53d6100\u5757\uff0c\u4e00\u4e2a\u5b58\u94b1100\u5757\u3002\u5047\u8bbe\u8d26\u6237\u539f\u672c\u67090\u5757\uff0c\u5982\u679c\u53d6\u94b1\u7ebf\u7a0b\u548c\u5b58\u94b1\u7ebf\u7a0b\u540c\u65f6\u53d1\u751f\uff0c\u4f1a\u51fa\u73b0\u4ec0\u4e48\u7ed3\u679c \u5462\uff1f\u53d6\u94b1\u4e0d\u6210\u529f\uff0c\u8d26\u6237\u4f59\u989d\u662f100.\u53d6\u94b1\u6210\u529f\u4e86\uff0c\u8d26\u6237\u4f59\u989d\u662f0.\u90a3\u5230\u5e95\u662f\u54ea\u4e2a\u5462\uff1f\u5f88\u96be\u8bf4\u6e05\u695a\u3002\u56e0\u6b64\u591a\u7ebf\u7a0b\u540c\u6b65\u5c31\u662f\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n \u4e09\u3001\u4e0d\u540c\u6b65\u65f6\u7684\u4ee3\u7801<\/strong><\/p>\n \u4ee3\u7801\u5f88\u7b80\u5355\uff0c\u6211\u5c31\u4e0d\u89e3\u91ca\u4e86\uff0c\u770b\u770b\u8fd0\u884c\u7ed3\u679c\u600e\u6837\u5462\uff1f\u622a\u53d6\u4e86\u5176\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u662f\u4e0d\u662f\u5f88\u4e71\uff0c\u6709\u5199\u770b\u4e0d\u61c2\u3002<\/p>\n \u4f59\u989d\u4e0d\u8db3 \u4f59\u989d\u4e0d\u8db3 1441790503354\u5b58\u8fdb\uff1a100 1441790504354\u5b58\u8fdb\uff1a100 1441790504354\u53d6\u51fa\uff1a100 1441790505355\u5b58\u8fdb\uff1a100 1441790505355\u53d6\u51fa\uff1a100 \u56db\u3001\u4f7f\u7528\u540c\u6b65\u65f6\u7684\u4ee3\u7801<\/strong><\/p>\n \uff081\uff09\u540c\u6b65\u65b9\u6cd5\uff1a<\/strong><\/p>\n \u5373\u6709synchronized\u5173\u952e\u5b57\u4fee\u9970\u7684\u65b9\u6cd5\u3002 \u7531\u4e8ejava\u7684\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5185\u7f6e\u9501\uff0c\u5f53\u7528\u6b64\u5173\u952e\u5b57\u4fee\u9970\u65b9\u6cd5\u65f6\uff0c\u5185\u7f6e\u9501\u4f1a\u4fdd\u62a4\u6574\u4e2a\u65b9\u6cd5\u3002\u5728\u8c03\u7528\u8be5\u65b9\u6cd5\u524d\uff0c\u9700\u8981\u83b7\u5f97\u5185\u7f6e\u9501\uff0c\u5426\u5219\u5c31\u5904\u4e8e\u963b\u585e\u72b6\u6001\u3002<\/p>\n \u4fee\u6539\u540e\u7684Bank.java<\/p>\n \u518d\u770b\u770b\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n \u4f59\u989d\u4e0d\u8db3 \u4f59\u989d\u4e0d\u8db3 1441790837380\u5b58\u8fdb\uff1a100 1441790838380\u53d6\u51fa\uff1a100 1441790839381\u53d6\u51fa\uff1a100 \u77ac\u95f4\u611f\u89c9\u53ef\u4ee5\u7406\u89e3\u4e86\u5427\u3002<\/p>\n \u6ce8\uff1a synchronized\u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u4fee\u9970\u9759\u6001\u65b9\u6cd5\uff0c\u6b64\u65f6\u5982\u679c\u8c03\u7528\u8be5\u9759\u6001\u65b9\u6cd5\uff0c\u5c06\u4f1a\u9501\u4f4f\u6574\u4e2a\u7c7b<\/p>\n \uff082\uff09\u540c\u6b65\u4ee3\u7801\u5757<\/strong><\/p>\n \u5373\u6709synchronized\u5173\u952e\u5b57\u4fee\u9970\u7684\u8bed\u53e5\u5757\u3002\u88ab\u8be5\u5173\u952e\u5b57\u4fee\u9970\u7684\u8bed\u53e5\u5757\u4f1a\u81ea\u52a8\u88ab\u52a0\u4e0a\u5185\u7f6e\u9501\uff0c\u4ece\u800c\u5b9e\u73b0\u540c\u6b65<\/p>\n Bank.java\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n \u4f59\u989d\u4e0d\u8db3 1441791806699\u5b58\u8fdb\uff1a100 1441791806700\u53d6\u51fa\uff1a100 1441791807699\u5b58\u8fdb\uff1a100 \u6548\u679c\u548c\u65b9\u6cd5\u4e00\u5dee\u4e0d\u591a\u3002<\/p>\n \u6ce8\uff1a\u540c\u6b65\u662f\u4e00\u79cd\u9ad8\u5f00\u9500\u7684\u64cd\u4f5c\uff0c\u56e0\u6b64\u5e94\u8be5\u5c3d\u91cf\u51cf\u5c11\u540c\u6b65\u7684\u5185\u5bb9\u3002\u901a\u5e38\u6ca1\u6709\u5fc5\u8981\u540c\u6b65\u6574\u4e2a\u65b9\u6cd5\uff0c\u4f7f\u7528synchronized\u4ee3\u7801\u5757\u540c\u6b65\u5173\u952e\u4ee3\u7801\u5373\u53ef\u3002<\/p>\n \uff083\uff09\u4f7f\u7528\u7279\u6b8a\u57df\u53d8\u91cf(<\/strong>Volatile)\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65<\/strong><\/p>\n a.volatile\u5173\u952e\u5b57\u4e3a\u57df\u53d8\u91cf\u7684\u8bbf\u95ee\u63d0\u4f9b\u4e86\u4e00\u79cd\u514d\u9501\u673a\u5236 Bank.java\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n \u8fd0\u884c\u6548\u679c\u600e\u6837\u5462\uff1f<\/p>\n \u4f59\u989d\u4e0d\u8db3 \u4f59\u989d\u4e0d\u8db3 1441792010959\u5b58\u8fdb\uff1a100 1441792011960\u53d6\u51fa\uff1a100 1441792011961\u5b58\u8fdb\uff1a100 \u662f\u4e0d\u662f\u53c8\u770b\u4e0d\u61c2\u4e86\uff0c\u53c8\u4e71\u4e86\u3002\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f\u5c31\u662f\u56e0\u4e3avolatile\u4e0d\u80fd\u4fdd\u8bc1\u539f\u5b50\u64cd\u4f5c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64volatile\u4e0d\u80fd\u4ee3\u66ff synchronized\u3002\u6b64\u5916volatile\u4f1a\u7ec4\u7ec7\u7f16\u8bd1\u5668\u5bf9\u4ee3\u7801\u4f18\u5316\uff0c\u56e0\u6b64\u80fd\u4e0d\u4f7f\u7528\u5b83\u5c31\u4e0d\u9002\u7528\u5b83\u5427\u3002\u5b83\u7684\u539f\u7406\u662f\u6bcf\u6b21\u8981\u7ebf\u7a0b\u8981\u8bbf\u95eevolatile\u4fee\u9970 \u7684\u53d8\u91cf\u65f6\u90fd\u662f\u4ece\u5185\u5b58\u4e2d\u8bfb\u53d6\uff0c\u800c\u4e0d\u662f\u5b58\u7f13\u5b58\u5f53\u4e2d\u8bfb\u53d6\uff0c\u56e0\u6b64\u6bcf\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5230\u7684\u53d8\u91cf\u503c\u90fd\u662f\u4e00\u6837\u7684\u3002\u8fd9\u6837\u5c31\u4fdd\u8bc1\u4e86\u540c\u6b65\u3002<\/p>\n \uff084\uff09\u4f7f\u7528\u91cd\u5165\u9501\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65<\/strong><\/p>\n \u5728JavaSE5.0\u4e2d\u65b0\u589e\u4e86\u4e00\u4e2ajava.util.concurrent\u5305\u6765\u652f\u6301\u540c\u6b65\u3002ReentrantLock\u7c7b\u662f\u53ef\u91cd\u5165\u3001\u4e92\u65a5\u3001\u5b9e\u73b0\u4e86Lock\u63a5\u53e3\u7684\u9501\uff0c \u5b83\u4e0e\u4f7f\u7528synchronized\u65b9\u6cd5\u548c\u5feb\u5177\u6709\u76f8\u540c\u7684\u57fa\u672c\u884c\u4e3a\u548c\u8bed\u4e49\uff0c\u5e76\u4e14\u6269\u5c55\u4e86\u5176\u80fd\u529b\u3002 \u8fd0\u884c\u6548\u679c\u600e\u4e48\u6837\u5462\uff1f<\/p>\n \u4f59\u989d\u4e0d\u8db3 \u4f59\u989d\u4e0d\u8db3 1441792891934\u5b58\u8fdb\uff1a100 1441792892935\u5b58\u8fdb\uff1a100 1441792892954\u53d6\u51fa\uff1a100 \u6548\u679c\u548c\u524d\u4e24\u79cd\u65b9\u6cd5\u5dee\u4e0d\u591a\u3002<\/p>\n \u5982\u679csynchronized\u5173\u952e\u5b57\u80fd\u6ee1\u8db3\u7528\u6237\u7684\u9700\u6c42\uff0c\u5c31\u7528synchronized\uff0c\u56e0\u4e3a\u5b83\u80fd\u7b80\u5316\u4ee3\u7801 \u3002\u5982\u679c\u9700\u8981\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\uff0c\u5c31\u7528ReentrantLock\u7c7b\uff0c\u6b64\u65f6\u8981\u6ce8\u610f\u53ca\u65f6\u91ca\u653e\u9501\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u6b7b\u9501\uff0c\u901a\u5e38\u5728finally\u4ee3\u7801\u91ca\u653e\u9501<\/p>\n \uff085\uff09\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65<\/strong><\/p>\n Bank.java\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n \u8fd0\u884c\u6548\u679c\uff1a<\/p>\n \u4f59\u989d\u4e0d\u8db3 \u4f59\u989d\u4e0d\u8db3 1441794247939\u5b58\u8fdb\uff1a100 \u4f59\u989d\u4e0d\u8db3 \u8d26\u6237\u4f59\u989d\uff1a200<\/p>\n \u4f59\u989d\u4e0d\u8db3 1441794249941\u5b58\u8fdb\uff1a100 \u770b\u4e86\u8fd0\u884c\u6548\u679c\uff0c\u4e00\u5f00\u59cb\u4e00\u5934\u96fe\u6c34\uff0c\u600e\u4e48\u53ea\u8ba9\u5b58\uff0c\u4e0d\u8ba9\u53d6\u554a\uff1f\u770b\u770bThreadLocal\u7684\u539f\u7406\uff1a<\/p>\n \u5982\u679c\u4f7f\u7528ThreadLocal\u7ba1\u7406\u53d8\u91cf\uff0c\u5219\u6bcf\u4e00\u4e2a\u4f7f\u7528\u8be5\u53d8\u91cf\u7684\u7ebf\u7a0b\u90fd\u83b7\u5f97\u8be5\u53d8\u91cf\u7684\u526f\u672c\uff0c\u526f\u672c\u4e4b\u95f4\u76f8\u4e92\u72ec\u7acb\uff0c\u8fd9\u6837\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u968f\u610f\u4fee\u6539\u81ea\u5df1\u7684\u53d8 \u91cf\u526f\u672c\uff0c\u800c\u4e0d\u4f1a\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u4ea7\u751f\u5f71\u54cd\u3002\u73b0\u5728\u660e\u767d\u4e86\u5427\uff0c\u539f\u6765\u6bcf\u4e2a\u7ebf\u7a0b\u8fd0\u884c\u7684\u90fd\u662f\u4e00\u4e2a\u526f\u672c\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b58\u94b1\u548c\u53d6\u94b1\u662f\u4e24\u4e2a\u8d26\u6237\uff0c\u77e5\u8bc6\u540d\u5b57\u76f8\u540c\u800c\u5df2\u3002\u6240\u4ee5\u5c31\u4f1a\u53d1\u751f\u4e0a\u9762 \u7684\u6548\u679c\u3002<\/p>\n ThreadLocal\u4e0e\u540c\u6b65\u673a\u5236<\/strong><\/p>\n a.ThreadLocal\u4e0e\u540c\u6b65\u673a\u5236\u90fd\u662f\u4e3a\u4e86\u89e3\u51b3\u591a\u7ebf\u7a0b\u4e2d\u76f8\u540c\u53d8\u91cf\u7684\u8bbf\u95ee\u51b2\u7a81\u95ee\u9898<\/pre>\n
\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a0
1441790838380\u5b58\u8fdb\uff1a100
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n<\/pre>\n
\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
b.\u4f7f\u7528volatile\u4fee\u9970\u57df\u76f8\u5f53\u4e8e\u544a\u8bc9\u865a\u62df\u673a\u8be5\u57df\u53ef\u80fd\u4f1a\u88ab\u5176\u4ed6\u7ebf\u7a0b\u66f4\u65b0
c.\u56e0\u6b64\u6bcf\u6b21\u4f7f\u7528\u8be5\u57df\u5c31\u8981\u91cd\u65b0\u8ba1\u7b97\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u5bc4\u5b58\u5668\u4e2d\u7684\u503c
d.volatile\u4e0d\u4f1a\u63d0\u4f9b\u4efb\u4f55\u539f\u5b50\u64cd\u4f5c\uff0c\u5b83\u4e5f\u4e0d\u80fd\u7528\u6765\u4fee\u9970final\u7c7b\u578b\u7684\u53d8\u91cf<\/p>\n<\/pre>\n
\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
ReenreantLock\u7c7b\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a
ReentrantLock() : \u521b\u5efa\u4e00\u4e2aReentrantLock\u5b9e\u4f8b
lock() : \u83b7\u5f97\u9501
unlock() : \u91ca\u653e\u9501
\u6ce8\uff1aReentrantLock()\u8fd8\u6709\u4e00\u4e2a\u53ef\u4ee5\u521b\u5efa\u516c\u5e73\u9501\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u4f46\u7531\u4e8e\u80fd\u5927\u5e45\u5ea6\u964d\u4f4e\u7a0b\u5e8f\u8fd0\u884c\u6548\u7387\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528
Bank.java\u4ee3\u7801\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n<\/pre>\n
\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a200<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n<\/pre>\n
\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a100<\/p>\n
1441794248940\u5b58\u8fdb\uff1a100
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a0<\/p>\n
\u8d26\u6237\u4f59\u989d\uff1a300<\/p>\n
b.\u524d\u8005\u91c7\u7528\u4ee5\u201d\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u7684\u65b9\u6cd5\uff0c\u540e\u8005\u91c7\u7528\u4ee5\u201d\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u7684\u65b9\u5f0f<\/p>\n","protected":false},"excerpt":{"rendered":"Java \u591a\u7ebf\u7a0b\u540c\u6b65\u7684\u4e94\u79cd\u65b9\u6cd5\u4e00\u3001\u5f15\u8a00\u524d\u51e0\u5929\u9762\u8bd5\uff0c\u88ab\u5927\u5e08\u8650\u6b8b\u4e86\uff0c\u597d\u591a\u57fa\u7840\u77e5\u8bc6\u5fc5\u987b\u5f97\u91cd\u65b0\u62ff\u8d77\u6765\u554a\u3002\u95f2\u8bdd\u4e0d\u591a\u8bf4\uff0c\u8fdb\u5165\u6b63\u9898\u3002\u4e8c\u3001...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/536"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=536"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/536\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}