{"id":543,"date":"2023-09-16T16:06:01","date_gmt":"2023-09-16T08:06:01","guid":{"rendered":""},"modified":"2023-09-17T00:41:54","modified_gmt":"2023-09-16T16:41:54","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%90%8c%e6%ad%a5%e7%9a%84%e4%b8%89%e7%a7%8d%e5%ae%9e%e7%8e%b0%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/mushiming.com\/543.html","title":{"rendered":"\u591a\u7ebf\u7a0b\u2014\u2014\u591a\u7ebf\u7a0b\u540c\u6b65\u7684\u4e09\u79cd\u5b9e\u73b0\u65b9\u6cd5"},"content":{"rendered":"
\u5f53\u4f7f\u7528\u591a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u4e2a\u8d44\u6e90\u7684\u65f6\u5019\uff0c\u975e\u5e38\u5bb9\u6613\u51fa\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\uff08\u4f8b\u5982\uff0c\u5f53\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5bf9\u4e00\u4e2a\u6570\u636e\u8fdb\u884c\u4fee\u6539\u7684\u65f6\u5019\uff0c\u4f1a\u5bfc\u81f4\u67d0\u4e9b\u7ebf\u7a0b\u5bf9\u6570\u636e\u7684\u4fee\u6539\u4e22\u5931\uff09\u3002<\/p>\n
\u56e0\u6b64\uff0c\u9700\u8981\u91c7\u7528\u540c\u6b65\u673a\u5236\u6765\u89e3\u51b3\u8fd9\u79cd\u95ee\u9898\u3002\u800cJava\u4e3b\u8981\u63d0\u4f9b\u4e86\u4e09\u79cd\u5b9e\u73b0\u540c\u6b65\u673a\u5236\u7684\u65b9\u6cd5\u3002\u4eca\u5929\u6211\u4eec\u5c31\u6765\u8ba4\u8bc6\u4e00\u4e0b~~<\/p>\n
\u4e00\u3001synchronized\u5173\u952e\u5b57<\/strong><\/p>\n \u5728Java\u8bed\u8a00\u4e2d\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5bf9\u8c61\u9501\u4e0e\u4e4b\u76f8\u5173\u8054\uff0c\u8be5\u9501\u8868\u660e\u5bf9\u8c61\u5728\u4efb\u4f55\u65f6\u5019\u53ea\u5141\u8bb8\u88ab\u4e00\u4e2a\u7ebf\u7a0b\u9501\u62e5\u6709\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u8c03\u7528\u5bf9\u8c61\u7684\u4e00\u6bb5synchronized\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u5148\u83b7\u53d6\u8fd9\u4e2a\u9501\uff0c\u7136\u540e\u53bb\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\uff0c\u6267\u884c\u7ed3\u675f\u4e4b\u540e\uff0c\u91ca\u653e\u9501\u3002<\/p>\n \u800csynchronized\u5173\u952e\u5b57\u4e3b\u8981\u6709\u4e24\u79cd\u7528\u6cd5\uff1asynchronized\u65b9\u6cd5<\/strong>\u548csynchronized\u5757<\/strong>\u3002\u6b64\u5916\uff0c\u8fd9\u4e2a\u5173\u952e\u5b57\u8fd8\u53ef\u4ee5\u4f5c\u7528\u4e8e\u9759\u6001\u65b9\u6cd5\u3001\u7c7b\u6216\u8005\u67d0\u4e2a\u5b9e\u4f8b\uff0c\u4f46\u8fd9\u90fd\u5bf9\u7a0b\u5e8f\u7684\u6548\u7387\u6709\u5f88\u5927\u7684\u5f71\u54cd<\/strong>\u3002<\/p>\n 1.synchronized\u65b9\u6cd5\u3002\u5728\u65b9\u6cd5\u7684\u58f0\u660e\u524d\u4e3b\u8981\u6709synchronized\u5173\u952e\u5b57\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n \u53ea\u8981\u628a\u591a\u4e2a\u7ebf\u7a0b\u5bf9\u7c7b\u9700\u8981\u88ab\u540c\u6b65\u7684\u8d44\u6e90\u7684\u64cd\u4f5c\u653e\u5230mutiThreadAccess()\u65b9\u6cd5\u4e2d\uff0c\u5c31\u80fd\u4fdd\u8bc1\u8fd9\u4e2a\u65b9\u6cd5\u5728\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u88ab\u540c\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e86\u591a\u7ebf\u7a0b\u8bbf\u95ee\u7684\u5b89\u5168\u6027\u3002\u7136\u800c\uff0c\u5f53\u4e00\u4e2a\u65b9\u6cd5\u7684\u65b9\u6cd5\u4f53\u89c4\u6a21\u975e\u5e38\u5927\u65f6\uff0c\u628a\u8be5\u65b9\u6cd5\u58f0\u660e\u4e3asynchronized\u4f1a\u5927\u5927\u5f71\u54cd\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002\u4e3a\u4e86\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\uff0cJava\u63d0\u4f9b\u4e86synchronized\u5757\u3002<\/p>\n 2.synchronized\u5757<\/p>\n synchronized\u5757\u65e2\u53ef\u4ee5\u628a\u4efb\u610f\u7684\u4ee3\u7801\u6bb5\u58f0\u660e\u4e3asynchronized\uff0c\u4e5f\u53ef\u4ee5\u6307\u5b9a\u4e0a\u9501\u7684\u5bf9\u8c61\uff0c\u6709\u975e\u5e38\u9ad8\u7684\u7075\u6d3b\u6027\u3002\u5176\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n \u4e8c\u3001wait()\u65b9\u6cd5\u4e0enotify()\u65b9\u6cd5<\/strong><\/p>\n \u5f53\u4f7f\u7528synchronized\u6765\u4fee\u9970\u67d0\u4e2a\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u5982\u679c\u7ebf\u7a0bA1\u5728\u6267\u884csynchronized\u4ee3\u7801\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0bA2\u4e5f\u8981\u540c\u65f6\u6267\u884c\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u540c\u4e00synchronized\u4ee3\u7801\u65f6\uff0c\u7ebf\u7a0bA2\u5c06\u8981\u7b49\u5230\u7ebf\u7a0bA1\u6267\u884c\u5b8c\u6210\u4e4b\u540e\uff0c\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4f7f\u7528wait()\u65b9\u6cd5\u548cnotify()\u65b9\u6cd5\u3002<\/p>\n \u5728synchronized\u4ee3\u7801\u88ab\u6267\u884c\u671f\u95f4\uff0c\u7ebf\u7a0b\u53ef\u4ee5\u8c03\u7528\u5bf9\u8c61\u7684wait()\u65b9\u6cd5\uff0c\u91ca\u653e\u5bf9\u8c61\u9501\uff0c\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\uff0c\u5e76\u4e14\u53ef\u4ee5\u8c03\u7528notify()\u65b9\u6cd5\u6216\u8005notifyAll()\u65b9\u6cd5\u901a\u77e5\u6b63\u5728\u7b49\u5f85\u7684\u5176\u4ed6\u7ebf\u7a0b\u3002notify()\u65b9\u6cd5\u4ec5\u5524\u9192\u4e00\u4e2a\u7ebf\u7a0b\uff08\u5373\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\uff09\uff0c\u5e76\u5141\u8bb8\u5b83\u53bb\u83b7\u5f97\u9501\uff0c<\/strong>notifyAll()\u65b9\u6cd5\u5524\u9192\u6240\u6709\u7b49\u5f85\u8fd9\u4e2a\u5bf9\u8c61\u7684\u7ebf\u7a0b\u5e76\u5141\u8bb8\u4ed6\u4eec\u53bb\u83b7\u5f97\u9501\uff0c\u4f46\u5e76\u4e0d\u662f\u8ba9\u6240\u6709\u5524\u9192\u7ebf\u7a0b\u90fd\u53bb\u83b7\u53d6\u5230\u9501\uff0c\u800c\u662f\u8ba9\u4ed6\u4eec\u53bb\u7ade\u4e89\u3002<\/strong><\/p>\n \u4e09\u3001Lock<\/strong><\/p>\n JDK5\u65b0\u589e\u52a0\u4e86Lock\u63a5\u53e3\u4ee5\u53ca\u5b83\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7bReentrantLock(\u91cd\u5165\u9501)\uff0cLock\u4e5f\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u3002\u5177\u4f53\u800c\u8a00\uff0c\u5b83\u63d0\u4f9b\u4e86\u5982\u4e0b\u4e00\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\uff1a<\/p>\n 1.lock()\u3002<\/p>\n \u8fd9\u4e2a\u65b9\u6cd5\u4ee5\u963b\u585e\u7684\u65b9\u5f0f\u83b7\u53d6\u9501\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u83b7\u53d6\u5230\u9501\u4e86\uff0c\u7acb\u5373\u8fd4\u56de\uff1b\u5982\u679c\u522b\u7684\u7ebf\u7a0b\u6301\u6709\u9501\uff0c\u5f53\u524d\u7ebf\u7a0b\u5c31\u7b49\u5f85\uff0c\u77e5\u9053\u83b7\u53d6\u5230\u9501\u4e4b\u540e\u8fd4\u56de\u3002<\/p>\n 2.tryLock()\u3002<\/p>\n \u8fd9\u4e2a\u65b9\u6cd5\u4e0elock()\u65b9\u6cd5\u4e0d\u540c\uff0c\u5b83\u4ee5\u975e\u963b\u585e\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u9501\u3002\u6b64\u5916\uff0c\u5b83\u53ea\u662f\u5e38\u8bc6\u6027\u5730\u53bb\u83b7\u53d6\u4e00\u4e0b\u9501\uff0c\u5982\u679c\u83b7\u53d6\u5230\u4e86\u9501\uff0c\u7acb\u5373\u8fd4\u56detrue\uff0c\u5426\u5219\u7acb\u5373\u8fd4\u56defalse\u3002<\/p>\n 3.tryLock(long timeout,TimeUnit unit)<\/p>\n \u5982\u679c\u83b7\u53d6\u5230\u9501\uff0c\u7acb\u5373\u8fd4\u56detrue\uff0c\u5426\u5219\u4f1a\u7b49\u5f85\u53c2\u6570\u7ed9\u5b9a\u7684\u65f6\u95f4\u5355\u5143\uff0c\u5728\u7b49\u5f85\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u83b7\u53d6\u5230\u4e86\u9501\uff0c\u5c31\u8fd4\u56detrue\uff0c\u5982\u679c\u7b49\u5f85\u8d85\u65f6\u5219\u8fd4\u56defalse\u3002<\/p>\n 4.lockInterruptibly()<\/p>\n \u5982\u679c\u83b7\u53d6\u4e86\u9501\uff0c\u7acb\u5373\u8fd4\u56de\uff1b\u5982\u679c\u6ca1\u6709\u83b7\u53d6\u5230\u9501\uff0c\u5f53\u524d\u7ebf\u7a0b\u5904\u4e8e\u4f11\u7720\u72b6\u6001\uff0c\u76f4\u5230\u83b7\u53d6\u5230\u9501\uff0c\u6216\u8005\u5f53\u524d\u7ebf\u7a0b\u88ab\u522b\u7684\u7ebf\u7a0b\u4e2d\u65ad\uff08\u4f1a\u53d7\u5230InterruptedException\u5f02\u5e38\uff09\u3002\u5b83\u4e0elock()\u65b9\u6cd5\u6700\u5927\u7684\u533a\u522b\u5728\u4e8e\u5982\u679clock()\u65b9\u6cd5\u83b7\u53d6\u4e0d\u5230\u9501\u5c31\u4f1a\u4e00\u76f4\u5904\u4e8e\u963b\u585e\u72b6\u6001\uff0c\u800c\u4e14\u8fd8\u4f1a\u5ffd\u7565interrupt\uff08\uff09\u65b9\u6cd5\u3002<\/p>\n \u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a <\/p>\n <\/p>\n \u5982\u679c\u628alock.lockInterruptibly()\u66ff\u6362\u4e3alock.lock\uff08\uff09\uff0c\u7f16\u8bd1\u5668\u5c06\u4f1a\u63d0\u793alock.lock()catch\u4ee3\u7801\u5757\u65e0\u6548\uff0c\u8fd9\u662f\u56e0\u4e3alock.lock\uff08\uff09\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u7531\u6b64\u53ef\u89c1lock()\u65b9\u6cd5\u4f1a\u5ffd\u7565interrupt()\u5f15\u53d1\u7684\u5f02\u5e38\u3002<\/p>\n <\/p>\n \u597d\u5566\uff0c\u4ee5\u4e0a\u5c31\u662f\u5b9e\u73b0Java\u591a\u7ebf\u7a0b\u540c\u6b65\u7684\u4e09\u79cd\u65b9\u6cd5\u7684\u76f8\u5173\u603b\u7ed3\uff0c\u5982\u679c\u5927\u5bb6\u6709\u4ec0\u4e48\u66f4\u5177\u4f53\u7684\u53d1\u73b0\u6216\u8005\u53d1\u73b0\u6587\u4e2d\u6709\u63cf\u8ff0\u9519\u8bef\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u8bc4\u8bba\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\u5440~~<\/p>\n <\/p>\n Biu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~pia\uff01<\/strong><\/em><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"\u591a\u7ebf\u7a0b\u2014\u2014\u591a\u7ebf\u7a0b\u540c\u6b65\u7684\u4e09\u79cd\u5b9e\u73b0\u65b9\u6cd5\u5f53\u4f7f\u7528\u591a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u4e2a\u8d44\u6e90\u7684\u65f6\u5019\uff0c\u975e\u5e38\u5bb9\u6613\u51fa\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\uff08\u4f8b\u5982\uff0c\u5f53\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5bf9...","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\/543"}],"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=543"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/543\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}public synchronized void mutiThreadAccess\uff08\uff09\uff1b<\/code><\/pre>\n
synchronized\uff08syncObject\uff09{\n\n\/\/\u8bbf\u95eesynchObject\u7684\u4ee3\u7801\n\n}<\/code><\/pre>\n
package javatest;\n\nimport java.util.concurrent.locks.Lock;\nimport java.util.concurrent.locks.ReentrantLock;\n\npublic class LockTest {\n\n\tpublic static void main(String[] args)throws InterruptedException {\n\t\t\/\/ TODO Auto-generated method stub\n\t\tfinal Lock lock=new ReentrantLock();\n\t\tlock.lock();\n\t\tThread t1=new Thread(new Runnable() {\n\t\t\tpublic void run() {\n\t\t\t\ttry {\n\t\t\t\t\tlock.lockInterruptibly();\n\t\t\t\t}catch(InterruptedException e)\n\t\t\t\t{\n\t\t\t\t\tSystem.out.println(\"interrupted\");\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t\tt1.start();\n\t\tt1.interrupt();\n\t\tThread.sleep(1);\n\t}\n}\n<\/code><\/pre>\n