{"id":551,"date":"2023-09-16T15:50:01","date_gmt":"2023-09-16T07:50:01","guid":{"rendered":""},"modified":"2023-09-17T00:42:12","modified_gmt":"2023-09-16T16:42:12","slug":"%e5%b8%a6%e4%bd%a0%e6%b7%b1%e5%ba%a6%e5%88%a8%e6%9e%90%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%9a%84%e5%90%8c%e6%ad%a5%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"https:\/\/mushiming.com\/551.html","title":{"rendered":"\u5e26\u4f60\u6df1\u5ea6\u5228\u6790\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u63a7\u5236"},"content":{"rendered":"

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

\u591a\u7ebf\u7a0b\u7684\u56e2\u961f\u534f\u4f5c\uff1a\u540c\u6b65\u63a7\u5236<\/h1>\n

\u540c\u6b65\u63a7\u5236\u662f\u5e76\u53d1\u7a0b\u5e8f\u5fc5\u4e0d\u53ef\u5c11\u7684\u91cd\u8981\u624b\u6bb5\u3002\u4e4b\u524d\u4ecb\u7ecd\u7684\u5173\u952e\u5b57synchronized\u5c31\u662f\u4e00\u79cd\u6700\u7b80\u5355\u7684\u63a7\u5236\u65b9\u6cd5\uff0c\u5b83\u51b3\u5b9a\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u662f\u5426\u53ef\u4ee5\u8bbf\u95ee\u4e34\u754c\u533a\u8d44\u6e90\u3002\u540c\u65f6\uff0cObject.wait()\u65b9\u6cd5\u548cObject.notify)\u65b9\u6cd5\u8d77\u5230\u4e86\u7ebf\u7a0b\u7b49\u5f85\u548c\u901a\u77e5\u7684\u4f5c\u7528\u3002\u8fd9\u4e9b\u5de5\u5177\u5bf9\u4e8e\u5b9e\u73b0\u590d\u6742\u7684\u591a\u7ebf\u7a0b\u534f\u4f5c\u8d77\u5230\u4e86\u91cd\u8981\u7684\u4f5c\u7528\u3002\u4e0b\u9762\u6211\u4eec\u9996\u5148\u5c06\u4ecb\u7ecd\u5173\u952e\u5b57synchronized\u3001Object.wait()\u65b9\u6cd5\u548cObject.notify()\u65b9\u6cd5\u7684\u66ff\u4ee3\u54c1\uff08\u6216\u8005\u8bf4\u662f\u589e\u5f3a\u7248\uff09\u2014\u2014\u91cd\u5165\u9501\u3002<\/p>\n

\u4e00\u3001\u5173\u952e\u5b57synchronized\u7684\u529f\u80fd\u6269\u5c55:\u91cd\u5165\u9501<\/h2>\n
    \n
  1. \u91cd\u5165\u9501\u53ef\u4ee5\u5b8c\u5168\u66ff\u4ee3\u5173\u952e\u5b57synchronized\u3002\u5728JDK
    5.0\u7684\u65e9\u671f\u7248\u672c\u4e2d\uff0c\u91cd\u5165\u9501\u7684\u6027\u80fd\u8fdc\u8fdc\u4f18\u4e8e\u5173\u952e\u5b57synchronized\uff0c\u4f46\u4eceJDK 6.0\u5f00\u59cb\uff0cJDK\u5728\u5173\u952e\u5b57synchronized\u4e0a\u505a\u4e86\u5927\u91cf\u7684\u4f18\u5316\uff0c\u4f7f\u5f97\u4e24\u8005\u7684\u6027\u80fd\u5dee\u8ddd\u5e76\u4e0d\u5927\u3002<\/li>\n
  2. \u91cd\u5165\u9501\u4f7f\u7528java.util.concurrent.locks.ReentrantLock\u7c7b\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u6bb5\u6700\u7b80\u5355\u7684\u91cd\u5165\u9501\u4f7f\u7528\u6848\u4f8b\u3002<\/li>\n<\/ol>\n
    public<\/span> class<\/span> ReenterLock<\/span> implements<\/span> Runnable<\/span>{ \n   <\/span>\npublic<\/span> static<\/span> ReentrantLock<\/span> lock=<\/span>new<\/span> ReentrantLock<\/span>(<\/span>)<\/span>;<\/span>\npublic<\/span> static<\/span> int<\/span> i=<\/span>0<\/span>;<\/span>\n@override<\/span>\npublic<\/span> void<\/span> run<\/span>(<\/span>)<\/span>{ \n   <\/span>\nfor<\/span>(<\/span>int<\/span> j=<\/span>0<\/span>;<\/span>j<<\/span>10000000<\/span>;<\/span>j++<\/span>)<\/span>{ \n   <\/span>\nlock.<\/span>lock<\/span>(<\/span>)<\/span>;<\/span>\ntry<\/span>{ \n   <\/span>\ni++<\/span>;<\/span>}<\/span>finally<\/span>{ \n   <\/span>\nlock.<\/span>unlock<\/span>(<\/span>)<\/span>;<\/span>\n  }<\/span>\n }<\/span>\n}<\/span>\npublic<\/span> static<\/span> void<\/span> main (<\/span>String<\/span>[<\/span>]<\/span> args)<\/span> throws<\/span> \nInterruptedException<\/span> { \n   <\/span>\nReenterLock<\/span> tl=<\/span>new<\/span> ReenterLock<\/span>(<\/span>)<\/span>;<\/span>\nThread<\/span> t1=<\/span>new<\/span> Thread<\/span> (<\/span>tl)<\/span>;<\/span>\nThread<\/span> t2=<\/span>new<\/span> Thread<\/span>(<\/span>tl)<\/span>;<\/span>\nt1.<\/span>start<\/span>(<\/span>)<\/span>;<\/span>\nt2.<\/span>start<\/span>(<\/span>)<\/span>;<\/span>\nt1.<\/span>join<\/span>(<\/span>)<\/span>;<\/span>\nt2.<\/span>join<\/span>(<\/span>)<\/span>;<\/span>\nSystem<\/span>.<\/span>out.<\/span>println<\/span>(<\/span>i)<\/span>;<\/span>\n}<\/span>}<\/span>\n\n<\/code><\/pre>\n
      \n
    1. \u4e0a\u8ff0\u4ee3\u7801\u7b2c7~12\u884c\u4f7f\u7528\u91cd\u5165\u9501\u4fdd\u62a4\u4e34\u754c\u533a\u8d44\u6e90i\uff0c\u786e\u4fdd\u591a\u7ebf\u7a0b\u5bf9i\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u3002\u4ece\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u770b\u5230\uff0c\u4e0e\u5173\u952e\u5b57synchronized\u76f8\u6bd4\uff0c\u91cd\u5165\u9501\u6709\u7740\u663e\u793a\u7684\u64cd\u4f5c\u8fc7\u7a0b\u3002\u5f00\u53d1\u4eba\u5458\u5fc5\u987b\u624b\u52a8\u6307\u5b9a\u4f55\u65f6\u52a0\u9501\uff0c\u4f55\u65f6\u91ca\u653e\u9501\u3002\u4e5f\u6b63\u56e0\u4e3a\u8fd9\u6837\uff0c\u91cd\u5165\u9501\u5bf9\u903b\u8f91\u63a7\u5236\u7684\u7075\u6d3b\u6027\u8981\u8fdc\u8fdc\u4f18\u4e8e\u5173\u952e\u5b57synchronized\u3002\u4f46\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5728\u9000\u51fa\u4e34\u754c\u533a\u65f6\uff0c\u5fc5\u987b\u8bb0\u5f97\u91ca\u653e\u9501(\u4ee3\u7801\u7b2c11\u884c)\uff0c\u5426\u5219\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5c31\u6ca1\u6709\u673a\u4f1a\u518d\u8bbf\u95ee\u4e34\u754c\u533a\u4e86\u3002<\/li>\n
    2. \u4f60\u53ef\u80fd\u4f1a\u5bf9\u91cd\u5165\u9501\u7684\u540d\u5b57\u611f\u5230\u5947\u602a\u3002\u9501\u4e3a\u4ec0\u4e48\u8981\u52a0\u4e0a\u201c\u91cd\u5165\u201d\u4e24\u4e2a\u5b57\u5462?\u4ece\u7c7b\u7684\u547d\u540d\u4e0a\u770b\uff0cRe-
      Entrant-Lock\u7ffb\u8bd1\u6210\u91cd\u5165\u9501\u975e\u5e38\u8d34\u5207\u3002\u4e4b\u6240\u4ee5\u8fd9\u4e48\u53eb\uff0c\u662f\u56e0\u4e3a\u8fd9\u79cd\u9501\u662f\u53ef\u4ee5\u53cd\u590d\u8fdb\u5165\u7684\u3002\u5f53\u7136\uff0c\u8fd9\u91cc\u7684\u53cd\u590d\u4ec5\u4ec5\u5c40\u9650\u4e8e\u4e00\u4e2a\u7ebf\u7a0b\u3002\u4e0a\u8ff0\u4ee3\u7801\u7684\u7b2c7~12\u884c\uff0c\u53ef\u4ee5\u5199\u6210\u4e0b\u9762\u7684\u5f62\u5f0f:<\/li>\n<\/ol>\n
      lock.<\/span>lock<\/span>(<\/span>)<\/span>;<\/span>lock.<\/span>lock<\/span>(<\/span>)<\/span>;<\/span>try<\/span>{ \n   <\/span>\ni++<\/span>;<\/span>}<\/span>finally<\/span>{ \n   <\/span>\nlock.<\/span> unlock<\/span>(<\/span>)<\/span>;<\/span>lock.<\/span>unlock<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n
        \n
      1. \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u8fde\u7eed\u4e24\u6b21\u83b7\u5f97\u540c\u4e00\u628a\u9501\u662f\u5141\u8bb8\u7684\u3002\u5982\u679c\u4e0d\u5141\u8bb8\u8fd9\u4e48\u64cd\u4f5c\uff0c\u90a3\u4e48\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5728\u7b2c2\u6b21\u83b7\u5f97\u9501\u65f6\uff0c\u5c06\u4f1a\u548c\u81ea\u5df1\u4ea7\u751f\u6b7b\u9501\u3002\u7a0b\u5e8f\u5c31\u4f1a\u201c\u5361\u6b7b\u201d\u5728\u7b2c2\u6b21\u7533\u8bf7\u9501\u7684\u8fc7\u7a0b\u4e2d\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u540c\u4e00\u4e2a\u7ebf\u7a0b\u591a\u6b21\u83b7\u5f97\u9501\uff0c\u90a3\u4e48\u5728\u91ca\u653e\u9501\u7684\u65f6\u5019\uff0c\u4e5f\u5fc5\u987b\u91ca\u653e\u76f8\u540c\u6b21\u6570\u3002\u5982\u679c\u91ca\u653e\u9501\u7684\u6b21\u6570\u591a\u4e86\uff0c\u90a3\u4e48\u4f1a\u5f97\u5230\u4e00\u4e2ajava.lang.IllegalMonitorStateException\u5f02\u5e38\uff0c\u53cd\u4e4b\uff0c\u5982\u679c\u91ca\u653e\u9501\u7684\u6b21\u6570\u5c11\u4e86\uff0c\u90a3\u4e48\u76f8\u5f53\u4e8e\u7ebf\u7a0b\u8fd8\u6301\u6709\u8fd9\u4e2a\u9501\uff0c\u56e0\u6b64\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4e5f\u65e0\u6cd5\u8fdb\u5165\u4e34\u754c\u533a\u3002<\/li>\n
      2. \u9664\u4f7f\u7528\u4e0a\u7684\u7075\u6d3b\u6027\u4ee5\u5916\uff0c\u91cd\u5165\u9501\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\u3002\u6bd4\u5982\uff0c\u91cd\u5165\u9501\u53ef\u4ee5\u63d0\u4f9b\u4e2d\u65ad\u5904\u7406\u7684\u80fd\u529b\u3002<\/li>\n<\/ol>\n

        \u4e00\u3001\u4e2d\u65ad\u54cd\u5e94<\/h3>\n
          \n
        1. \u5bf9\u4e8e\u5173\u952e\u5b57synchronized\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u5728\u7b49\u5f85\u9501\uff0c\u90a3\u4e48\u7ed3\u679c\u53ea\u6709\u4e24\u79cd\u60c5\u51b5\uff0c\u8981\u4e48\u5b83\u83b7\u5f97\u8fd9\u628a\u9501\u7ee7\u7eed\u6267\u884c\uff0c\u8981\u4e48\u5b83\u5c31\u4fdd\u6301\u7b49\u5f85\u3002\u800c\u4f7f\u7528\u91cd\u5165\u9501\uff0c\u5219\u63d0\u4f9b\u53e6\u5916\u4e00\u79cd\u53ef\u80fd\uff0c\u90a3\u5c31\u662f\u7ebf\u7a0b\u53ef\u4ee5\u88ab\u4e2d\u65ad\u3002\u4e5f\u5c31\u662f\u5728\u7b49\u5f85\u9501\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u6839\u636e\u9700\u8981\u53d6\u6d88\u5bf9\u9501\u7684\u8bf7\u6c42\u3002\u6709\u4e9b\u65f6\u5019\uff0c\u8fd9\u4e48\u505a\u662f\u975e\u5e38\u6709\u5fc5\u8981\u7684\u3002\u6bd4\u5982\uff0c\u4f60\u548c\u670b\u53cb\u7ea6\u597d\u4e00\u8d77\u53bb\u6253\u7403\uff0c\u5982\u679c\u4f60\u7b49\u4e86\u534a\u4e2a\u5c0f\u65f6\u670b\u53cb\u8fd8\u6ca1\u6709\u5230\uff0c\u4f60\u7a81\u7136\u63a5\u5230\u4e00\u4e2a\u7535\u8bdd\uff0c\u8bf4\u7531\u4e8e\u7a81\u53d1\u60c5\u51b5\uff0c\u670b\u53cb\u4e0d\u80fd\u5982\u7ea6\u524d\u6765\u4e86\uff0c\u90a3\u4e48\u4f60\u4e00\u5b9a\u626b\u5174\u5730\u6253\u9053\u56de\u5e9c\u4e86\u3002\u4e2d\u65ad\u6b63\u662f\u63d0\u4f9b\u4e86\u4e00\u5957\u7c7b\u4f3c\u7684\u673a\u5236\u3002\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u6b63\u5728\u7b49\u5f85\u9501\uff0c\u90a3\u4e48\u5b83\u4f9d\u7136\u53ef\u4ee5\u6536\u5230\u4e00\u4e2a\u901a\u77e5\uff0c\u88ab\u544a\u77e5\u65e0\u987b\u7b49\u5f85\uff0c\u53ef\u4ee5\u505c\u6b62\u5de5\u4f5c\u4e86\u3002\u8fd9\u79cd\u60c5\u51b5\u5bf9\u4e8e\u5904\u7406\u6b7b\u9501\u662f\u6709\u4e00\u5b9a\u5e2e\u52a9\u7684\u3002<\/li>\n
        2. \u4e0b\u9762\u7684\u4ee3\u7801\u4ea7\u751f\u4e86\u4e00\u4e2a\u6b7b\u9501\uff0c\u4f46\u5f97\u76ca\u4e8e\u9501\u4e2d\u65ad\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u8f7b\u6613\u5730\u89e3\u51b3\u8fd9\u4e2a\u6b7b\u9501\u3002<\/li>\n<\/ol>\n
          public<\/span> class<\/span> IntLock<\/span> implements<\/span> Runnable<\/span>{ \n   <\/span>\npublic<\/span> static<\/span> ReentrantLock<\/span> lockl =<\/span> new<\/span> ReentrantLock<\/span>(<\/span>)<\/span>;<\/span>public<\/span> static<\/span> ReentrantLock<\/span> lock2 =<\/span> new<\/span> ReentrantLock<\/span>(<\/span>)<\/span>;<\/span>int<\/span> lock;<\/span>\n\/** \u2605\u63a7\u5236\u52a0\u9501\u987a\u5e8f,\u65b9\u4fbf\u6784\u9020\u6b7b\u9501*param lock *\/<\/span>\npublic<\/span> IntLock<\/span>(<\/span>int<\/span> lock)<\/span>{ \n   <\/span>\nthis<\/span>.<\/span>lock =<\/span> lock;<\/span>\n}<\/span>\n@override<\/span>\npublic<\/span> void<\/span> run<\/span>(<\/span>)<\/span>{ \n   <\/span>\ntry<\/span> { \n   <\/span>\nif<\/span> (<\/span>lock ==<\/span> 1<\/span>)<\/span>{ \n   <\/span>\nlock1.<\/span> lockInterruptibly<\/span>(<\/span>)<\/span>;<\/span>try<\/span>{ \n   <\/span>\nThread<\/span>.<\/span>sleep<\/span>(<\/span>500<\/span>)<\/span>;<\/span>\n}<\/span>catch<\/span> (<\/span>InterruptedException<\/span> e)<\/span>{ \n   <\/span>1l<\/span>ock2.<\/span> lockInterruptiblyO)<\/span>;<\/span>\n}<\/span>else<\/span>{ \n   <\/span>\nlock2 .<\/span> lockInterruptibly<\/span>(<\/span>)<\/span>;<\/span>try<\/span>{ \n   <\/span>\nThread<\/span>.<\/span>sleep (<\/span>500<\/span>)<\/span>;<\/span>\n}<\/span>catch<\/span> (<\/span>InterruptedException<\/span> e)<\/span>{ \n   <\/span>}<\/span>lock1.<\/span> lockInterruptiblyO)<\/span>;<\/span>\n]<\/span> catch<\/span> (<\/span>InterruptedException<\/span> e)<\/span>{ \n   <\/span>\ne.<\/span>printStackTrace<\/span>(<\/span>;<\/span>\n]<\/span>finally<\/span>{ \n   <\/span>\nif<\/span>(<\/span>lock1.<\/span>isHeldByCurrentThread (<\/span>)<\/span>)<\/span>\nlockl.<\/span>unlock (<\/span>)<\/span>;<\/span>\nif<\/span> (<\/span>lock2.<\/span>isHeldByCurrentThread<\/span>(<\/span>)<\/span>\nlock2.<\/span>unlock<\/span>(<\/span>;<\/span>\nSystem<\/span>.<\/span>out.<\/span>println (<\/span>Thread<\/span>.<\/span>currentThread (<\/span>)<\/span>.<\/span>getId<\/span>(<\/span>)<\/span>+<\/span>\":\u7ebf\u7a0b\u9000\u51fa\"<\/span>)<\/span>;<\/span>\npublic<\/span> static<\/span> void<\/span> main<\/span>(<\/span>String<\/span>[<\/span>]<\/span> args)<\/span> throws<\/span> InterruptedException<\/span>{ \n   <\/span>\nIntLock<\/span> r1 =<\/span> new<\/span> IntLock<\/span> (<\/span>1<\/span>)<\/span>;<\/span>\nIntLock<\/span> r2=<\/span> new<\/span> IntLock<\/span>(<\/span>2<\/span>)<\/span>;<\/span>Thread<\/span> tl =<\/span> new<\/span> Thread<\/span> (<\/span>r1)<\/span> ;<\/span>Thread<\/span> t2 =<\/span> new<\/span> Thread<\/span>(<\/span>r2)<\/span>;<\/span>t1.<\/span>start<\/span>(<\/span>)<\/span>;<\/span>t2.<\/span>start<\/span>(<\/span>)<\/span>;<\/span>\nThread<\/span>.<\/span>sleep<\/span>(<\/span>1000<\/span>)<\/span>;<\/span>\/\/\u4e2d\u65ad\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b<\/span>\nt2.<\/span>interrupt (<\/span>)<\/span>;<\/span>\n}<\/span>}<\/span>\n\n<\/code><\/pre>\n
            \n
          1. \u7ebf\u7a0bt1\u548c
            t2\u542f\u52a8\u540e\uff0ct1\u5148\u5360\u7528lock1\uff0c\u518d\u5360\u7528lock2;t2\u5148\u5360\u7528lock2\uff0c\u518d\u8bf7\u6c42lock1.\u56e0\u6b64,\u5f88\u5bb9\u6613\u5f62\u6210t1\u548ct2\u4e4b\u95f4\u7684\u76f8\u4e92\u7b49\u5f85\u3002\u5728\u8fd9\u91cc,\u5bf9\u9501\u7684\u8bf7\u6c42,\u7edf\u4e00\u4f7f\u7528lockInterruptibly(\u65b9\u6cd5\u3002\u8fd9\u662f\u4e00\u4e2a\u53ef\u4ee5\u5bf9\u4e2d\u65ad\u8fdb\u884c\u54cd\u5e94\u7684\u9501\u7533\u8bf7\u52a8\u4f5c\uff0c\u5373\u5728\u7b49\u5f85\u9501\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u54cd\u5e94\u4e2d\u65ad\u3002<\/li>\n
          2. \u5728\u4ee3\u7801\u7b2c47\u884c\uff0c\u4e3b\u7ebf\u7a0bmain
            \u5904\u4e8e\u4f11\u7720\u72b6\u6001\uff0c\u6b64\u65f6\uff0c\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u5904\u4e8e\u6b7b\u9501\u7684\u72b6\u6001\u3002\u5728\u4ee3\u7801\u7b2c49\u884c\uff0c\u7531\u4e8et2\u7ebf\u7a0b\u88ab\u4e2d\u65ad\uff0c\u6545t2\u4f1a\u653e\u5f03\u5bf9lock1\u7684\u7533\u8bf7\uff0c\u540c\u65f6\u91ca\u653e\u5df2\u83b7\u5f97\u7684lock2\u3002\u8fd9\u4e2a\u64cd\u4f5c\u5bfc\u81f4t1\u7ebf\u7a0b\u53ef\u4ee5\u987a\u5229\u5f97\u5230lock2\u800c\u7ee7\u7eed\u6267\u884c\u4e0b\u53bb\u3002<\/li>\n
          3. \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u5c06\u8f93\u51fa:<\/li>\n<\/ol>\n
            java.<\/span>lang.<\/span><\/span> InterruptedException<\/span>\nat java.<\/span>util.<\/span>concurrent.<\/span> locks.<\/span><\/span>AbstractQueuedSynchronizer<\/span>.<\/span>doAcquireInterruptibly (<\/span>AbstractQueuedSynchronizer<\/span>.<\/span>java:<\/span>898<\/span>)<\/span>\nat java.<\/span>util.<\/span>concurrent.<\/span> locks.<\/span><\/span> AbstractQueuedSynchronizer<\/span>.<\/span>acquireInterruptibly<\/span>(<\/span>AbstractQueuedSynchronizer<\/span>.<\/span>java:<\/span>1222<\/span>)<\/span>\nat java.<\/span>util.<\/span>concurrent.<\/span> locks.<\/span><\/span>ReentrantLock<\/span>.<\/span>lockInterruptibly<\/span>(<\/span>ReentrantLock<\/span>.<\/span>java:<\/span>335<\/span>)<\/span>\nat geym.<\/span>conc.<\/span>ch3.<\/span>synctrl.<\/span><\/span>IntLock<\/span>.<\/span> run (<\/span>IntLock<\/span>.<\/span>java:<\/span> 31<\/span>)<\/span>at java.<\/span>lang.<\/span><\/span> Thread<\/span>.<\/span>run<\/span>(<\/span>Thread<\/span>.<\/span>java:<\/span>745<\/span>)<\/span>\n9<\/span>:<\/span>\u7ebf\u7a0b\u9000\u51fa\n8<\/span>:<\/span>\u7ebf\u7a0b\u9000\u51fa\n\n<\/code><\/pre>\n
              \n
            1. \u53ef\u4ee5\u770b\u5230\uff0c\u4e2d\u65ad\u540e\u4e24\u4e2a\u7ebf\u7a0b\u53cc\u53cc\u9000\u51fa\uff0c\u4f46\u771f\u6b63\u5b8c\u6210\u5de5\u4f5c\u7684\u53ea\u6709tl\uff0c\u800ct2\u7ebf\u7a0b\u5219\u653e\u5f03\u5176\u4efb\u52a1\u76f4\u63a5\u9000\u51fa\uff0c\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<\/ol>\n

              \u4e8c\u3001\u9501\u7533\u8bf7\u7b49\u5f85\u9650\u65f6<\/h3>\n
                \n
              1. \u9664\u4e86\u7b49\u5f85\u5916\u90e8\u901a\u77e5\u4e4b\u5916\uff0c\u8981\u907f\u514d\u6b7b\u9501\u8fd8\u6709\u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\uff0c\u90a3\u5c31\u662f\u9650\u65f6\u7b49\u5f85\u3002\u4f9d\u7136\u4ee5\u7ea6\u670b\u53cb\u6253\u7403\u4e3a\u4f8b\uff0c\u5982\u679c\u670b\u53cb\u8fdf\u8fdf\u4e0d\u6765\uff0c\u53c8\u65e0\u6cd5\u8054\u7cfb\u5230\u4ed6\uff0c\u90a3\u4e48\u5728\u7b49\u5f851\u52302\u4e2a\u5c0f\u65f6\u540e\uff0c\u6211\u60f3\u5927\u90e8\u5206\u4eba\u90fd\u4f1a\u626b\u5174\u79bb\u53bb\u3002\u5bf9\u7ebf\u7a0b\u6765\u8bf4\u4e5f\u662f\u8fd9\u6837\u3002\u901a\u5e38\uff0c\u6211\u4eec\u65e0\u6cd5\u5224\u65ad\u4e3a\u4ec0\u4e48\u4e00\u4e2a\u7ebf\u7a0b\u8fdf\u8fdf\u62ff\u4e0d\u5230\u9501\u3002\u4e5f\u8bb8\u662f\u56e0\u4e3a\u6b7b\u9501\u4e86\uff0c\u4e5f\u8bb8\u662f\u56e0\u4e3a\u4ea7\u751f\u4e86\u9965\u997f\u3002\u5982\u679c\u7ed9\u5b9a\u4e00\u4e2a\u7b49\u5f85\u65f6\u95f4\uff0c\u8ba9\u7ebf\u7a0b\u81ea\u52a8\u653e\u5f03\uff0c\u90a3\u4e48\u5bf9\u7cfb\u7edf\u6765\u8bf4\u662f\u6709\u610f\u4e49\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528tryLock()\u65b9\u6cd5\u8fdb\u884c\u4e00\u6b21\u9650\u65f6\u7684\u7b49\u5f85\u3002<\/li>\n
              2. \u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u9650\u65f6\u7b49\u5f85\u9501\u7684\u4f7f\u7528\u3002<\/li>\n<\/ol>\n
                public<\/span> class<\/span> TimeLock<\/span> implements<\/span> Runnable<\/span>{ \n   <\/span>\npublic<\/span> static<\/span> ReentrantLock<\/span> lock=<\/span>new<\/span> ReentrantLock<\/span>(<\/span>)<\/span>;<\/span>@Override<\/span>\npublic<\/span> void<\/span> run<\/span>(<\/span>)<\/span>{ \n   <\/span>\ntry<\/span>{ \n   <\/span>\nif<\/span>(<\/span>lock.<\/span>tryLock (<\/span>5<\/span>,<\/span>TimeUnit<\/span>.<\/span>SECONDS)<\/span>)<\/span>\nThread<\/span>.<\/span>sleep<\/span>(<\/span>6000<\/span>)<\/span>;<\/span>\n}<\/span>else<\/span>{ \n   <\/span>\nSystem<\/span>.<\/span>out.<\/span>println (<\/span>\"get lock failed\"<\/span>)<\/span>;<\/span>\n}<\/span>catch<\/span> (<\/span>InterruptedException<\/span> e)<\/span>{ \n   <\/span>\ne.<\/span>printstackTrace<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span>finally<\/span>{ \n   <\/span>if<\/span> (<\/span>lock.<\/span>isHeldByCurrentThread0)<\/span> lock.<\/span>unlock<\/span>(<\/span>)<\/span>;<\/span>}<\/span>\npublic<\/span> static<\/span> void<\/span> main<\/span>(<\/span>String<\/span>[<\/span>]<\/span> args)<\/span>{ \n   <\/span>\nTimeLock<\/span> tl=<\/span>new<\/span> TimeLock<\/span>(<\/span>)<\/span>;<\/span>\nThread<\/span> tl=<\/span>new<\/span> Thread<\/span>(<\/span>tl)<\/span>;<\/span>Thread<\/span> t2=<\/span>new<\/span> Thread<\/span>(<\/span>tl)<\/span>;<\/span>t1.<\/span>start<\/span>(<\/span>)<\/span>;<\/span>\nt2.<\/span>start<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span>}<\/span>\n<\/code><\/pre>\n
                  \n
                1. \u5728\u8fd9\u91cc\uff0ctryLock()\u65b9\u6cd5\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u8868\u793a\u7b49\u5f85\u65f6\u957f\uff0c\u53e6\u5916\u4e00\u4e2a\u8868\u793a\u8ba1\u65f6\u5355\u4f4d\u3002\u8fd9\u91cc\u7684\u5355\u4f4d\u8bbe\u7f6e\u4e3a\u79d2\uff0c\u65f6\u957f\u4e3a5\uff0c\u8868\u793a\u7ebf\u7a0b\u5728\u8fd9\u4e2a\u9501\u8bf7\u6c42\u4e2d\u6700\u591a\u7b49\u5f855\u79d2\u3002\u5982\u679c\u8d85\u8fc75\u79d2\u8fd8\u6ca1\u6709\u5f97\u5230\u9501\uff0c\u5c31\u4f1a\u8fd4\u56defalse\u3002\u5982\u679c\u6210\u529f\u83b7\u5f97\u9501\uff0c\u5219\u8fd4\u56detrue\u3002<\/li>\n
                2. \u5728\u672c\u4f8b\u4e2d\uff0c\u7531\u4e8e\u5360\u7528\u9501\u7684\u7ebf\u7a0b\u4f1a\u6301\u6709\u9501\u957f\u8fbe6\u79d2\uff0c\u6545\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u65e0\u6cd5\u57285\u79d2\u7684\u7b49\u5f85\u65f6\u95f4\u5185\u83b7\u5f97\u9501\uff0c\u56e0\u6b64\u8bf7\u6c42\u9501\u4f1a\u5931\u8d25\u3002<\/li>\n
                3. ReentrantLock.tryLock()\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u4e0d\u5e26\u53c2\u6570\u76f4\u63a5\u8fd0\u884c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5f53\u524d\u7ebf\u7a0b\u4f1a\u5c1d\u8bd5\u83b7\u5f97\u9501\uff0c\u5982\u679c\u9501\u5e76\u672a\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5360\u7528\uff0c\u5219\u7533\u8bf7\u9501\u4f1a\u6210\u529f\uff0c\u5e76\u7acb\u5373\u8fd4\u56detrue\u3002\u5982\u679c\u9501\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5360\u7528\uff0c\u5219\u5f53\u524d\u7ebf\u7a0b\u4e0d\u4f1a\u8fdb\u884c\u7b49\u5f85\uff0c\u800c\u662f\u7acb\u5373\u8fd4\u56defalse\u3002\u8fd9\u79cd\u6a21\u5f0f\u4e0d\u4f1a\u5f15\u8d77\u7ebf\u7a0b\u7b49\u5f85\uff0c\u56e0\u6b64\u4e5f\u4e0d\u4f1a\u4ea7\u751f\u6b7b\u9501\u3002\u4e0b\u9762\u6f14\u793a\u4e86\u8fd9\u79cd\u4f7f\u7528\u65b9\u5f0f:<\/li>\n<\/ol>\n
                  public<\/span> class<\/span> TryLock<\/span> implements<\/span> Runnable<\/span>{ \n   <\/span>\npublic<\/span> static<\/span> ReentrantLock<\/span> lock1 -<\/span> new<\/span> ReentrantLock<\/span> (<\/span>;<\/span>public<\/span> static<\/span> ReentrantLock<\/span> lock2 =<\/span> new<\/span> ReentrantLock<\/span>(<\/span>;<\/span>int<\/span> lock;<\/span>\npublic<\/span> TeyLock<\/span> (<\/span>int<\/span> lock)<\/span>{ \n   <\/span>\nthis<\/span>.<\/span>lock =<\/span> lock;<\/span>\n@override<\/span>\npublic<\/span> void<\/span> run (<\/span> { \n   <\/span>\nif<\/span>(<\/span>1<\/span>ock --<\/span> 1<\/span>)<\/span>{ \n   <\/span>\nwhile<\/span> (<\/span>true<\/span>)<\/span> [<\/span>\nif<\/span>(<\/span>lockl.<\/span>tryLock0{ \n   <\/span>\ntryf\ntry<\/span> { \n   <\/span>\nThread<\/span>.<\/span>sleep (<\/span>500<\/span>)<\/span>;<\/span>\n}<\/span>catch<\/span> (<\/span>InterruptedException<\/span> e)<\/span>[<\/span>)<\/span>\nif<\/span> (<\/span>lock2.<\/span>tryLock<\/span>(<\/span>)<\/span>)<\/span>{ \n   <\/span>\ntry<\/span>{ \n   <\/span>\nSystem<\/span>.<\/span>out.<\/span>println\/<\/span>Thread<\/span>.<\/span>currentThread<\/span>(<\/span>)<\/span>\n.<\/span>getId<\/span>(<\/span> +<\/span>\":Ny Job done\"<\/span>]<\/span>;<\/span>\nreturn<\/span>;<\/span>\n}<\/span>finally<\/span>{ \n   <\/span>\nlock2.<\/span>unlock<\/span>(<\/span>)<\/span> ;<\/span>\n}<\/span>}<\/span>}<\/span>\nfinally<\/span> { \n   <\/span>\nlock1.<\/span>unlock<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span>\n}<\/span> else<\/span>{ \n   <\/span>\nwhile<\/span> (<\/span>true<\/span>)<\/span> { \n   <\/span>\nif<\/span>(<\/span>lock2.<\/span>tryLock<\/span>(<\/span>)<\/span>)<\/span>{ \n   <\/span>\ntry<\/span> { \n   <\/span>\ntry<\/span> { \n   <\/span>\nThread<\/span>.<\/span>sleep<\/span>(<\/span>500<\/span>)<\/span>;<\/span>\n]<\/span> catch<\/span> (<\/span>InterruptedException<\/span> e)<\/span>{ \n   <\/span>\nif<\/span>(<\/span> lock1.<\/span>tryLock<\/span>(<\/span>)<\/span>{ \n   <\/span>\ntry<\/span> { \n   <\/span>\nSystem<\/span>.<\/span>out.<\/span>println (<\/span>Thread<\/span>.<\/span>currentThread<\/span>(<\/span>)<\/span>\n.<\/span> getId<\/span>(<\/span>)<\/span>+<\/span>\":My Job done\"<\/span>)<\/span>;<\/span>\nreturn<\/span>;<\/span>\n}<\/span> finally<\/span> { \n   <\/span>\nlock1.<\/span>unlock<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span>\n]<\/span>finally<\/span>-<\/span>{ \n   <\/span>\nlock2.<\/span>unlock<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span>\n}<\/span>\n}<\/span>\n}<\/span>\n}<\/span>\n\npublic<\/span> static<\/span> void<\/span> main<\/span>(<\/span>String<\/span>[<\/span>]<\/span> args)<\/span> throws<\/span> InterruptedException<\/span> { \n   <\/span>\nTryLock<\/span> r1 =<\/span> new<\/span> TryLock<\/span>(<\/span>1<\/span>)<\/span>;<\/span>\nTryLock<\/span> r2=<\/span> new<\/span> Trylock<\/span>(<\/span>2<\/span>)<\/span>;<\/span>Thread<\/span> tl =<\/span> new<\/span> Thread<\/span>(<\/span>r1)<\/span>;<\/span>Thread<\/span> t2 =<\/span> new<\/span> Thread<\/span>(<\/span>r2)<\/span>;<\/span>t1.<\/span>start<\/span>(<\/span>)<\/span>;<\/span>\nt2.<\/span>start<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span>\n}<\/span>\n<\/code><\/pre>\n
                    \n
                  1. \u4e0a\u8ff0\u4ee3\u7801\u91c7\u7528\u4e86\u975e\u5e38\u5bb9\u6613\u6b7b\u9501\u7684\u52a0\u9501\u987a\u5e8f\u3002\u4e5f\u5c31\u662f\u5148\u8ba9t1\u83b7\u5f97lock1,\u518d\u8ba9t2\u83b7\u5f97lock2,\u63a5\u7740\u505a\u53cd\u5411\u8bf7\u6c42\uff0c\u8ba9t1\u7533\u8bf7lock2\uff0ct2\u7533\u8bf7lock1\u3002\u5728\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4f1a\u5bfc\u81f4tl\u548ct2\u76f8\u4e92\u7b49\u5f85\uff0c\u4ece\u800c\u5f15\u8d77\u6b7b\u9501\u3002<\/li>\n
                  2. \u4f46\u662f\u4f7f\u7528tryLock()\u65b9\u6cd5\u540e\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u5927\u5927\u6539\u5584\u4e86\u3002\u7531\u4e8e\u7ebf\u7a0b\u4e0d\u4f1a\u50bb\u50bb\u5730\u7b49\u5f85\uff0c\u800c\u662f\u4e0d\u505c\u5730\u5c1d\u8bd5\uff0c\u56e0\u6b64\uff0c\u53ea\u8981\u6267\u884c\u8db3\u591f\u957f\u7684\u65f6\u95f4\uff0c\u7ebf\u7a0b\u603b\u662f\u4f1a\u5f97\u5230\u6240\u6709\u9700\u8981\u7684\u8d44\u6e90\uff0c\u4ece\u800c\u6b63\u5e38\u6267\u884c\uff08\u8fd9\u91cc\u4ee5\u7ebf\u7a0b\u540c\u65f6\u83b7\u5f97lock1\u548c lock2\u4e24\u628a\u9501\uff0c\u4f5c\u4e3a\u5176\u53ef\u4ee5\u6b63\u5e38\u6267\u884c\u7684\u6761\u4ef6\uff09\u3002\u5728\u540c\u65f6\u83b7\u5f97 lock1\u548c lock2\u540e\uff0c\u7ebf\u7a0b\u5c31\u6253\u5370\u51fa\u6807\u5fd7\u7740\u4efb\u52a1\u5b8c\u6210\u7684\u4fe1\u606f\u201cMy Jobdone\u201d\u3002<\/li>\n
                  3. \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u7b49\u5f85\u4e00\u4f1a\u513f\uff08\u7531\u4e8e\u7ebf\u7a0b\u4e2d\u5305\u542b\u4f11\u7720500
                    \u6beb\u79d2\u7684\u4ee3\u7801\uff09\u3002\u6700\u7ec8\u4f60\u8fd8\u662f\u53ef\u4ee5\u6b23\u559c\u5730\u770b\u5230\u7a0b\u5e8f\u6267\u884c\u5b8c\u6bd5\uff0c\u5e76\u4ea7\u751f\u5982\u4e0b\u8f93\u51fa\uff0c\u8868\u793a\u4e24\u4e2a\u7ebf\u7a0b\u53cc\u53cc\u6b63\u5e38\u6267\u884c\u3002<\/li>\n<\/ol>\n

                    \"\u5e26\u4f60\u6df1\u5ea6\u5228\u6790\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u63a7\u5236<\/p>\n

                    \u4e09\u3001\u516c\u5e73\u9501<\/h3>\n
                      \n
                    • \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9501\u7684\u7533\u8bf7\u90fd\u662f\u975e\u516c\u5e73\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u7ebf\u7a0b1\u9996\u5148\u8bf7\u6c42\u4e86\u9501A\uff0c\u63a5\u7740\u7ebf\u7a0b\u2489\u4e5f\u8bf7\u6c42\u4e86\u9501A\u3002\u90a3\u4e48\u5f53\u9501A\u53ef\u7528\u65f6,\u662f\u7ebf\u7a0b1\u53ef\u4ee5\u83b7\u5f97\u9501\u8fd8\u662f\u7ebf\u7a0b2\u53ef\u4ee5\u83b7\u5f97\u9501\u5462?\u8fd9\u662f\u4e0d\u4e00\u5b9a\u7684\uff0c\u7cfb\u7edf\u53ea\u662f\u4f1a\u4ece\u8fd9\u4e2a\u9501\u7684\u7b49\u5f85\u961f\u5217\u4e2d\u968f\u673a\u6311\u9009\u4e00\u4e2a\u3002\u56e0\u6b64\u4e0d\u80fd\u4fdd\u8bc1\u5176\u516c\u5e73\u6027\u3002\u8fd9\u5c31\u597d\u6bd4\u4e70\u7968\u4e0d\u6392\u961f\uff0c\u5927\u5bb6\u90fd\u56f4\u5728\u552e\u7968\u7a97\u53e3\u524d\uff0c\u552e\u7968\u5458\u5fd9\u5f97\u7126\u5934\u70c2\u989d\uff0c\u4e5f\u987e\u4e0d\u53ca\u8c01\u5148\u8c01\u540e\uff0c\u968f\u4fbf\u627e\u4e2a\u4eba\u51fa\u7968\u5c31\u5b8c\u4e8b\u4e86\u3002\u800c\u516c\u5e73\u7684\u9501\uff0c\u5219\u4e0d\u662f\u8fd9\u6837\uff0c\u5b83\u4f1a\u6309\u7167\u65f6\u95f4\u7684\u5148\u540e\u987a\u5e8f\uff0c\u4fdd\u8bc1\u5148\u5230\u8005\u5148\u5f97\uff0c\u540e\u5230\u8005\u540e\u5f97\u3002\u516c\u5e73\u9501\u7684\u4e00\u5927\u7279\u70b9\u662f:\u5b83\u4e0d\u4f1a\u4ea7\u751f\u9965\u997f\u73b0\u8c61\u3002\u53ea\u8981\u4f60\u6392\u961f\uff0c\u6700\u7ec8\u8fd8\u662f\u53ef\u4ee5\u7b49\u5230\u8d44\u6e90\u7684\u3002\u5982\u679c\u6211\u4eec\u4f7f\u7528synchronized\u5173\u952e\u5b57\u8fdb\u884c\u9501\u63a7\u5236\uff0c\u90a3\u4e48\u4ea7\u751f\u7684\u9501\u5c31\u662f\u975e\u516c\u5e73\u7684\u3002\u800c\u91cd\u5165\u9501\u5141\u8bb8\u6211\u4eec\u5bf9\u5176\u516c\u5e73\u6027\u8fdb\u884c\u8bbe\u7f6e\u3002\u5b83\u7684\u6784\u9020\u51fd\u6570\u5982\u4e0b:<\/li>\n<\/ul>\n

                      \"\u5e26\u4f60\u6df1\u5ea6\u5228\u6790\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u63a7\u5236<\/p>\n

                        \n
                      • \u5f53\u53c2\u6570fair\u4e3a
                        true\u65f6\uff0c\u8868\u793a\u9501\u662f\u516c\u5e73\u7684\u3002\u516c\u5e73\u9501\u770b\u8d77\u6765\u5f88\u4f18\u7f8e\uff0c\u4f46\u662f\u8981\u5b9e\u73b0\u516c\u5e73\u9501\u5fc5\u7136\u8981\u6c42\u7cfb\u7edf\u7ef4\u62a4\u4e00\u4e2a\u6709\u5e8f\u961f\u5217\uff0c\u56e0\u6b64\u516c\u5e73\u9501\u7684\u5b9e\u73b0\u6210\u672c\u6bd4\u8f83\u9ad8\uff0c\u6027\u80fd\u5374\u975e\u5e38\u4f4e\u4e0b\uff0c\u56e0\u6b64\uff0c\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u9501\u662f\u975e\u516c\u5e73\u7684\u3002\u5982\u679c\u6ca1\u6709\u7279\u522b\u7684\u9700\u6c42\uff0c\u5219\u4e0d\u9700\u8981\u4f7f\u7528\u516c\u5e73\u9501\u3002\u516c\u5e73\u9501\u548c\u975e\u516c\u5e73\u9501\u5728\u7ebf\u7a0b\u8c03\u5ea6\u8868\u73b0\u4e0a\u4e5f\u662f\u975e\u5e38\u4e0d\u4e00\u6837\u7684\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u5f88\u597d\u5730\u7a81\u51fa\u516c\u5e73\u9501\u7684\u7279\u70b9:
                        \u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0
                        \"\u5e26\u4f60\u6df1\u5ea6\u5228\u6790\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u63a7\u5236
                        \"\u5e26\u4f60\u6df1\u5ea6\u5228\u6790\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u63a7\u5236
                        \u4e0a\u8ff0\u4ee3\u7801\u7b2c\ufe56\u884c\u6307\u5b9a\u9501\u662f\u516c\u5e73\u7684\u3002\u63a5\u7740\uff0c\u7531t1\u548ct2\u4e24\u4e2a\u7ebf\u7a0b\u5206\u522b\u8bf7\u6c42\u8fd9\u628a\u9501\uff0c\u5e76\u4e14\u5728\u5f97\u5230\u9501\u540e\uff0c\u8fdb\u884c\u4e00\u4e2a\u63a7\u5236\u53f0\u7684\u8f93\u51fa\uff0c\u8868\u793a\u81ea\u5df1\u5f97\u5230\u4e86\u9501\u3002\u5728\u516c\u5e73\u9501\u7684\u60c5\u51b5\u4e0b\uff0c\u5f97\u5230\u7684\u8f93\u51fa\u901a\u5e38\u5982\u4e0b\u6240\u793a:
                        \"\u5e26\u4f60\u6df1\u5ea6\u5228\u6790\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u63a7\u5236
                        \u7531\u4e8e\u4ee3\u7801\u4f1a\u4ea7\u751f\u5927\u91cf\u8f93\u51fa\uff0c\u8fd9\u91cc\u53ea\u622a\u53d6\u90e8\u5206\u8fdb\u884c\u8bf4\u660e\u3002\u5728\u8fd9\u4e2a\u8f93\u51fa\u4e2d\uff0c\u5f88\u660e\u663e\u53ef\u4ee5\u770b\u5230\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u57fa\u672c\u4e0a\u662f\u4ea4\u66ff\u83b7\u5f97\u9501\u7684\uff0c\u51e0\u4e4e\u4e0d\u4f1a\u53d1\u751f\u540c\u4e00\u4e2a\u7ebf\u7a0b\u8fde\u7eed\u591a\u6b21\u83b7\u5f97\u9501\u7684\u53ef\u80fd\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e86\u516c\u5e73\u6027\u3002\u5982\u679c\u4e0d\u4f7f\u7528\u516c\u5e73\u9501\uff0c\u90a3\u4e48\u60c5\u51b5\u4f1a\u5b8c\u5168\u4e0d\u4e00\u6837\uff0c\u4e0b\u9762\u662f\u4f7f\u7528\u975e\u516c\u5e73\u9501\u65f6\u7684\u90e8\u5206\u8f93\u51fa:
                        \"\u5e26\u4f60\u6df1\u5ea6\u5228\u6790\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u63a7\u5236
                        \"\u5e26\u4f60\u6df1\u5ea6\u5228\u6790\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u63a7\u5236
                        \u53ef\u4ee5\u770b\u5230\uff0c\u6839\u636e\u7cfb\u7edf\u7684\u8c03\u5ea6\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u4f1a\u503e\u5411\u4e8e\u518d\u6b21\u83b7\u53d6\u5df2\u7ecf\u6301\u6709\u7684\u9501\uff0c\u8fd9\u79cd\u5206\u914d\u65b9\u5f0f\u662f\u9ad8\u6548\u7684\uff0c\u4f46\u662f\u65e0\u516c\u5e73\u6027\u53ef\u8a00\u3002\u5bf9\u4e0a\u9762ReentrantLock \u7684\u51e0\u4e2a\u91cd\u8981\u65b9\u6cd5\u6574\u7406\u5982\u4e0b\u3002<\/strong><\/li>\n
                      • lock():\u83b7\u5f97\u9501\uff0c\u5982\u679c\u9501\u5df2\u7ecf\u88ab\u5360\u7528\uff0c\u5219\u7b49\u5f85\u3002<\/li>\n
                      • lockInterruptibly():\u83b7\u5f97\u9501\uff0c\u4f46\u4f18\u5148\u54cd\u5e94\u4e2d\u65ad\u3002<\/li>\n
                      • tryLock():\u5c1d\u8bd5\u83b7\u5f97\u9501\uff0c\u5982\u679c\u6210\u529f\uff0c\u5219\u8fd4\u56detrue\uff0c\u5931\u8d25\u8fd4\u56defalse\u3002\u8be5\u65b9\u6cd5\u4e0d\u7b49\u5f85\uff0c\u7acb\u5373\u8fd4\u56de\u3002<\/li>\n
                      • tryLock(long time, TimeUnit unit):\u5728\u7ed9\u5b9a\u65f6\u95f4\u5185\u5c1d\u8bd5\u83b7\u5f97\u9501\u3002unlock():\u91ca\u653e\u9501\u3002<\/li>\n<\/ul>\n

                        \u5c31\u91cd\u5165\u9501\u7684\u5b9e\u73b0\u6765\u770b\uff0c\u5b83\u4e3b\u8981\u96c6\u4e2d\u5728Java\u5c42\u9762\u3002\u5728\u91cd\u5165\u9501\u7684\u5b9e\u73b0\u4e2d\uff0c\u4e3b\u8981\u5305\u542b\u4e09\u4e2a\u8981\u7d20\u3002<\/strong><\/p>\n

                          \n
                        1. \u7b2c\u4e00\uff0c\u539f\u5b50\u72b6\u6001\u3002\u539f\u5b50\u72b6\u6001\u4f7f\u7528CAS\u64cd\u4f5c\uff08\u5728\u7b2c4\u7ae0\u8fdb\u884c\u8be6\u7ec6\u8ba8\u8bba\uff09\u6765\u5b58\u50a8\u5f53\u524d\u9501\u7684\u72b6\u6001\uff0c\u5224\u65ad\u9501\u662f\u5426\u5df2\u7ecf\u88ab\u522b\u7684\u7ebf\u7a0b\u6301\u6709\u4e86\u3002<\/li>\n
                        2. \u7b2c\u4e8c\uff0c\u7b49\u5f85\u961f\u5217\u3002\u6240\u6709\u6ca1\u6709\u8bf7\u6c42\u5230\u9501\u7684\u7ebf\u7a0b\uff0c\u4f1a\u8fdb\u5165\u7b49\u5f85\u961f\u5217\u8fdb\u884c\u7b49\u5f85\u3002\u5f85\u6709\u7ebf\u7a0b\u91ca\u653e\u9501\u540e\uff0c\u7cfb\u7edf\u5c31\u80fd\u4ece\u7b49\u5f85\u961f\u5217\u4e2d\u5524\u9192\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u7ee7\u7eed\u5de5\u4f5c\u3002<\/li>\n
                        3. \u7b2c\u4e09\uff0c\u963b\u585e\u539f\u8bedpark()\u548c unpark()\uff0c\u7528\u6765\u6302\u8d77\u548c\u6062\u590d\u7ebf\u7a0b\u3002\u6ca1\u6709\u5f97\u5230\u9501\u7684\u7ebf\u7a0b\u5c06\u4f1a\u88ab\u6302\u8d77\u3002\u6709\u5173park)\u548cunpark()\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u53c2\u8003\u7b2c3.1.7\u8282\u7ebf\u7a0b\u963b\u585e\u5de5\u5177\u7c7b:LockSupport\u3002<\/li>\n<\/ol>\n

                          \u6458\u81eaJAVA\u9ad8\u5e76\u53d1\u7a0b\u5e8f\u8bbe\u8ba1\uff0c\u63a8\u8350\u63a8\u8350<\/p>\n","protected":false},"excerpt":{"rendered":"\u5e26\u4f60\u6df1\u5ea6\u5228\u6790\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u63a7\u5236\u52a0\u5f3a\u5bf9\u591a\u7ebf\u7a0b\u7684\u7406\u89e3\uff0c\u91cd\u70b9\u8bb2\u89e3\u591a\u7ebf\u7a0b\u540c\u6b65\u63a7\u5236_\u591a\u7ebf\u7a0b\u540c\u6b65\u63a7\u5236","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\/551"}],"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=551"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/551\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}