{"id":534,"date":"2023-09-16T16:22:01","date_gmt":"2023-09-16T08:22: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","status":"publish","type":"post","link":"https:\/\/mushiming.com\/534.html","title":{"rendered":"Java \u591a\u7ebf\u7a0b\u540c\u6b65"},"content":{"rendered":"

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

Java \u7ebf\u7a0b\u540c\u6b65\u662f Java \u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\u3002\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u5982\u679c\u6ca1\u6709\u5408\u9002\u7684\u540c\u6b65\u673a\u5236\uff0c\u4f1a\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u6216\u8005\u7a0b\u5e8f\u5f02\u5e38\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd Java \u7ebf\u7a0b\u540c\u6b65\u7684\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528 Java \u4ee3\u7801\u6765\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\u3002<\/p>\n

Java \u7ebf\u7a0b\u540c\u6b65<\/h3>\n

Java \u7ebf\u7a0b\u540c\u6b65\u662f\u6307\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u901a\u8fc7\u4e92\u65a5\u8bbf\u95ee\u7684\u65b9\u5f0f\u4fdd\u8bc1\u6570\u636e\u7684\u6b63\u786e\u6027\u3002Java \u4e2d\u63d0\u4f9b\u4e86\u591a\u79cd\u540c\u6b65\u673a\u5236\uff0c\u5305\u62ec synchronized \u5173\u952e\u5b57\u3001Lock \u63a5\u53e3\u548c AtomicInteger \u7b49\u7c7b\u3002<\/p>\n

synchronized \u5173\u952e\u5b57<\/h4>\n

synchronized \u5173\u952e\u5b57\u662f Java \u7ebf\u7a0b\u540c\u6b65\u7684\u6700\u57fa\u672c\u673a\u5236\u3002synchronized \u5173\u952e\u5b57\u53ef\u4ee5\u7528\u4e8e\u65b9\u6cd5\u6216\u4ee3\u7801\u5757\u7684\u540c\u6b65\u3002<\/p>\n

    \n
  1. \u65b9\u6cd5\u540c\u6b65\uff1a\u5728\u65b9\u6cd5\u58f0\u660e\u4e2d\u4f7f\u7528 synchronized \u5173\u952e\u5b57\u53ef\u4ee5\u4f7f\u5f97\u6574\u4e2a\u65b9\u6cd5\u90fd\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n
    public<\/span> synchronized<\/span> void<\/span> method<\/span>(<\/span>)<\/span> { \n   <\/span>\n    \/\/ do something<\/span>\n}<\/span>\n<\/code><\/pre>\n

    \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6574\u4e2a method() \u65b9\u6cd5\u90fd\u662f\u540c\u6b65\u7684\uff0c\u4efb\u4f55\u7ebf\u7a0b\u5728\u6267\u884c\u8be5\u65b9\u6cd5\u65f6\uff0c\u90fd\u4f1a\u5148\u83b7\u53d6\u8be5\u5bf9\u8c61\u7684\u9501\u624d\u80fd\u6267\u884c\u3002<\/p>\n

      \n
    1. \u4ee3\u7801\u5757\u540c\u6b65\uff1a\u5728\u4ee3\u7801\u5757\u4e2d\u4f7f\u7528 synchronized \u5173\u952e\u5b57\u53ef\u4ee5\u4f7f\u5f97\u4ee3\u7801\u5757\u4e2d\u7684\u8bed\u53e5\u540c\u6b65\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n
      synchronized<\/span> (<\/span>obj)<\/span> { \n   <\/span>\n    \/\/ do something<\/span>\n}<\/span>\n<\/code><\/pre>\n

      \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cobj \u8868\u793a\u9700\u8981\u540c\u6b65\u7684\u5bf9\u8c61\uff0c\u53ea\u6709\u83b7\u53d6\u4e86 obj \u5bf9\u8c61\u7684\u9501\uff0c\u624d\u80fd\u6267\u884c\u4ee3\u7801\u5757\u4e2d\u7684\u8bed\u53e5\u3002<\/p>\n

      Lock \u63a5\u53e3<\/h4>\n

      \u9664\u4e86 synchronized \u5173\u952e\u5b57\u4e4b\u5916\uff0cJava \u8fd8\u63d0\u4f9b\u4e86 Lock \u63a5\u53e3\u6765\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\u3002Lock \u63a5\u53e3\u63d0\u4f9b\u4e86\u66f4\u52a0\u7075\u6d3b\u7684\u540c\u6b65\u673a\u5236\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u7ec6\u7c92\u5ea6\u7684\u540c\u6b65\u63a7\u5236\u3002Lock \u63a5\u53e3\u7684\u5e38\u7528\u5b9e\u73b0\u7c7b\u662f ReentrantLock\u3002<\/p>\n

        \n
      1. Lock \u63a5\u53e3\u7684\u4f7f\u7528\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n
        Lock<\/span> lock =<\/span> new<\/span> ReentrantLock<\/span>(<\/span>)<\/span>;<\/span>\nlock.<\/span>lock<\/span>(<\/span>)<\/span>;<\/span>\ntry<\/span> { \n   <\/span>\n    \/\/ do something<\/span>\n}<\/span> finally<\/span> { \n   <\/span>\n    lock.<\/span>unlock<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n

        \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a ReentrantLock \u5bf9\u8c61\uff0c\u5e76\u8c03\u7528 lock() \u65b9\u6cd5\u83b7\u5f97\u9501\u3002\u7136\u540e\uff0c\u5728 try-catch-finally \u8bed\u53e5\u5757\u4e2d\u6267\u884c\u9700\u8981\u540c\u6b65\u7684\u4ee3\u7801\u3002\u6700\u540e\uff0c\u5728 finally \u8bed\u53e5\u5757\u4e2d\u8c03\u7528 unlock() \u65b9\u6cd5\u91ca\u653e\u9501\u3002<\/p>\n

        AtomicInteger \u7c7b<\/h4>\n

        \u9664\u4e86 synchronized \u5173\u952e\u5b57\u548c Lock \u63a5\u53e3\u4e4b\u5916\uff0cJava \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u539f\u5b50\u7c7b\uff0c\u4f8b\u5982 AtomicInteger\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u539f\u5b50\u64cd\u4f5c\u3002\u539f\u5b50\u64cd\u4f5c\u662f\u6307\u4e0d\u53ef\u4e2d\u65ad\u7684\u5355\u4e2a\u64cd\u4f5c\uff0c\u56e0\u6b64\u53ef\u4ee5\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u8bbf\u95ee\u65f6\u7684\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n

          \n
        1. AtomicInteger \u7684\u4f7f\u7528\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n
          AtomicInteger<\/span> count =<\/span> new<\/span> AtomicInteger<\/span>(<\/span>0<\/span>)<\/span>;<\/span>\ncount.<\/span>incrementAndGet<\/span>(<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

          \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a AtomicInteger \u5bf9\u8c61\uff0c\u5e76\u521d\u59cb\u5316\u4e3a 0\u3002\u7136\u540e\uff0c\u8c03\u7528 incrementAndGet() \u65b9\u6cd5\u5b9e\u73b0\u539f\u5b50\u81ea\u589e\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u8bbf\u95ee\u65f6\u7684\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n

          \u7ebf\u7a0b\u540c\u6b65\u793a\u4f8b\u4ee3\u7801<\/h3>\n

          \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 Java \u4ee3\u7801\u6f14\u793a\u7ebf\u7a0b\u540c\u6b65\u7684\u793a\u4f8b\u7a0b\u5e8f\uff1a<\/p>\n

          public<\/span> class<\/span> ThreadSyncExample<\/span> implements<\/span> Runnable<\/span> { \n   <\/span>\n\n    private<\/span> static<\/span> int<\/span> count =<\/span> 0<\/span>;<\/span>\n    private<\/span> static<\/span> Object<\/span> lock =<\/span> new<\/span> Object<\/span>(<\/span>)<\/span>;<\/span>\n\n    public<\/span> void<\/span> run<\/span>(<\/span>)<\/span> { \n   <\/span>\n        synchronized<\/span> (<\/span>lock)<\/span> { \n   <\/span>\n            for<\/span> (<\/span>int<\/span> i =<\/span> 0<\/span>;<\/span> i <<\/span> 1000000<\/span>;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n                count++<\/span>;<\/span>\n            }<\/span>\n        }<\/span>\n    }<\/span>\n\n    public<\/span> static<\/span> void<\/span> main<\/span>(<\/span>String<\/span>[<\/span>]<\/span> args)<\/span> throws<\/span> InterruptedException<\/span> { \n   <\/span>\n        Thread<\/span> t1 =<\/span> new<\/span> Thread<\/span>(<\/span>new<\/span> ThreadSyncExample<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n        Thread<\/span> t2 =<\/span> new<\/span> Thread<\/span>(<\/span>new<\/span> ThreadSyncExample<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n        t1.<\/span>start<\/span>(<\/span>)<\/span>;<\/span>\n        t2.<\/span>start<\/span>(<\/span>)<\/span>;<\/span>\n        t1.<\/span>join<\/span>(<\/span>)<\/span>;<\/span>\n        t2.<\/span>join<\/span>(<\/span>)<\/span>;<\/span>\n        System<\/span>.<\/span>out.<\/span>println<\/span>(<\/span>\"count = \"<\/span> +<\/span> count)<\/span>;<\/span>\n    }<\/span>\n}<\/span>\n<\/code><\/pre>\n

          \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5b9e\u73b0\u4e86 Runnable \u63a5\u53e3\u7684\u7c7b ThreadSyncExample\u3002\u5728 run() \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 synchronized \u5173\u952e\u5b57\u5c06\u9700\u8981\u540c\u6b65\u7684\u4ee3\u7801\u5757\u8fdb\u884c\u540c\u6b65\uff0c\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u8bbf\u95ee\u65f6\u7684\u7ebf\u7a0b\u5b89\u5168\u3002\u5728 main() \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u65b0\u7ebf\u7a0b\u5e76\u542f\u52a8\u5b83\u4eec\uff0c\u7136\u540e\u4f7f\u7528 join() \u65b9\u6cd5\u7b49\u5f85\u4e24\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u3002\u6700\u540e\uff0c\u6211\u4eec\u8f93\u51fa count \u7684\u503c\uff0c\u786e\u8ba4\u5b83\u7684\u503c\u4e3a 2000000\uff0c\u8bf4\u660e\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\u8d77\u5230\u4e86\u4f5c\u7528\u3002<\/p>\n

          \u603b\u7ed3<\/h3>\n

          \u672c\u6587\u4ecb\u7ecd\u4e86 Java \u7ebf\u7a0b\u540c\u6b65\u7684\u6982\u5ff5\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528 Java \u4ee3\u7801\u6765\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\u3002Java \u7ebf\u7a0b\u540c\u6b65\u662f Java \u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u638c\u63e1\u7ebf\u7a0b\u540c\u6b65\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u65f6\u7684\u6570\u636e\u6b63\u786e\u6027\u548c\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 synchronized \u5173\u952e\u5b57\u3001Lock \u63a5\u53e3\u548c\u539f\u5b50\u7c7b\u7b49\u591a\u79cd\u673a\u5236\u6765\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Java \u591a\u7ebf\u7a0b\u540c\u6b65\u672c\u6587\u4ecb\u7ecd\u4e86Java\u7ebf\u7a0b\u540c\u6b65\u7684\u6982\u5ff5\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Java\u4ee3\u7801\u6765\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\u3002Java\u7ebf\u7a0b\u540c\u6b65\u662fJava\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u975e...","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\/534"}],"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=534"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/534\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}