{"id":550,"date":"2023-09-16T15:52:01","date_gmt":"2023-09-16T07:52:01","guid":{"rendered":""},"modified":"2023-09-17T00:42:12","modified_gmt":"2023-09-16T16:42:12","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%90%8c%e6%ad%a5%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/mushiming.com\/550.html","title":{"rendered":"\u591a\u7ebf\u7a0b\u540c\u6b65\u5b9e\u73b0"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u4e00\u3001synchronized\u5173\u952e\u5b57<\/h2>\n

java\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\u548csynchronized\u5757\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\u3002<\/p>\n

1.synchronized\u65b9\u6cd5<\/h3>\n

\u5728\u65b9\u6cd5\u7684\u58f0\u660e\u524d\u4e3b\u8981\u6709synchronized\u5173\u952e\u5b57\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n

public<\/span> synchronized<\/span> void<\/span> mutiThreadAccess\uff08\uff09;<\/span>\n<\/code><\/pre>\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<\/h3>\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

synchronized<\/span>\uff08syncObject\uff09{ \n   <\/span>\n \n\/\/\u8bbf\u95eesynchObject\u7684\u4ee3\u7801<\/span>\n \n}<\/span>\n<\/code><\/pre>\n

\u4e8c\u3001volatile\u5173\u952e\u5b57<\/h2>\n

\u200bvolatile\u5173\u952e\u5b57\u4e3a\u57df\u53d8\u91cf\u7684\u8bbf\u95ee\u63d0\u4f9b\u4e86\u4e00\u79cd\u514d\u9501\u673a\u5236\uff0c \u4f7f\u7528volatile\u4fee\u9970\u57df\u76f8\u5f53\u4e8e\u544a\u8bc9JVM\u8be5\u57df\u53ef\u80fd\u4f1a\u88ab\u5176\u4ed6\u7ebf\u7a0b\u66f4\u65b0\uff0c \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\uff0cvolatile\u4e0d\u4f1a\u63d0\u4f9b\u4efb\u4f55\u539f\u5b50\u64cd\u4f5c\uff0c\u5b83\u4e5f\u4e0d\u80fd\u7528\u6765\u4fee\u9970final\u7c7b\u578b\u7684\u53d8\u91cf\u3002<\/p>\n

\u4e09\u3001wait()\u65b9\u6cd5\u4e0enotify()\u65b9\u6cd5<\/h2>\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\uff0cnotifyAll()\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<\/p>\n

\u56db\u3001Lock<\/h2>\n

JDK5\u65b0\u589e\u52a0\u4e86Lock\u63a5\u53e3\u4ee5\u53ca\u5b83\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7bReentranLock\uff0cLock\u4e5f\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u3002<\/p>\n

    \n
  1. \n

    \u200block()\u3002\u4ee5\u963b\u585e\u7684\u65b9\u5f0f\u83b7\u53d6\u9501\uff0c\u5982\u679c\u83b7\u53d6\u5230\u4e86\u9501\uff0c\u7acb\u5373\u8fd4\u56de\uff1b\u5982\u679c\u522b\u7684\u7ebf\u7a0b\u6301\u6709\u9501\uff0c\u5f53\u524d\u7ebf\u7a0b\u7b49\u5f85\uff0c\u76f4\u5230\u83b7\u53d6\u9501\u540e\u8fd4\u56de\u3002<\/p>\n<\/li>\n

  2. \n

    tryLock()\u3002\u4ee5\u975e\u963b\u585e\u7684\u65b9\u5f0f\u83b7\u53d6\u9501\u3002\u53ea\u662f\u5c1d\u8bd5\u6027\u5730\u53bb\u83b7\u53d6\u4e00\u4e0b\u9501\uff0c\u5982\u679c\u83b7\u53d6\u5230\u9501\uff0c\u7acb\u5373\u8fd4\u56detrue\uff0c\u5426\u5219\uff0c\u7acb\u5373\u8fd4\u56defalse\u3002<\/p>\n<\/li>\n

  3. \n

    tryLock(long timeout, TimeUnit unit)\u3002\u5982\u679c\u83b7\u53d6\u4e86\u9501\uff0c\u7acb\u5373\u8fd4\u56de\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\u4e86\u9501\uff0c\u5c31\u8fd4\u56detrue\uff0c\u5982\u679c\u7b49\u5f85\u8d85\u65f6\uff0c\u8fd4\u56defalse\u3002<\/p>\n<\/li>\n

  4. \n

    lockInterruptibly()\u3002\u5982\u679c\u83b7\u53d6\u4e86\u9501\uff0c\u7acb\u5373\u8fd4\u56de\uff0c\u5982\u679c\u6ca1\u6709\u83b7\u53d6\u9501\uff0c\u5f53\u524d\u7ebf\u7a0b\u5904\u4e8e\u4f11\u7720\u72b6\u6001\uff0c\u76f4\u5230\u83b7\u5f97\u9501\uff0c\u6216\u8005\u5f53\u524d\u7ebf\u7a0b\u88ab\u522b\u7684\u7ebf\u7a0b\u4e2d\u65ad(\u4f1a\u53d7\u5230InterruptedException\u5f02\u5e38)\u3002\u5b83\u4e0elock()\u65b9\u6cd5\u6700\u5927\u7684\u533a\u522b\u5728\u4e8e\u5982\u679clock()\u65b9\u6cd5\u83b7\u53d6\u4e0d\u5230\u9501\uff0c\u4f1a\u4e00\u76f4\u5904\u4e8e\u963b\u585e\u72b6\u6001\uff0c\u4e14\u4f1a\u5ffd\u7565interrupt()\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u591a\u7ebf\u7a0b\u540c\u6b65\u5b9e\u73b0\u4e00\u3001synchronized\u5173\u952e\u5b57java\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...","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\/550"}],"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=550"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/550\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}