{"id":205151,"date":"2025-05-29T17:28:44","date_gmt":"2025-05-29T09:28:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205151\/"},"modified":"2025-05-29T17:28:44","modified_gmt":"2025-05-29T09:28:44","slug":"python%e7%ba%bf%e7%a8%8b%e9%87%8d%e5%a4%8d%e6%89%a7%e8%a1%8c%e9%97%ae%e9%a2%98%ef%bc%9a-%e4%b8%ba%e4%bb%80%e4%b9%88%e7%a8%8b%e5%ba%8f%e6%89%a7%e8%a1%8c%e7%bb%93%e6%9d%9f%e6%97%b6%e6%89%93%e5%8d%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205151\/","title":{"rendered":"Python\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a \u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u6267\u884c\u7ed3\u675f\u65f6\u6253\u5370\u4e86\u591a\u4e2a\u201cThread-5\u201d\uff0c\u5374\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u771f\u6b63\u6267\u884c\u4e86\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a \u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u6267\u884c\u7ed3\u675f\u65f6\u6253\u5370\u4e86\u591a\u4e2a\u201cThread-5\u201d\uff0c\u5374\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u771f\u6b63\u6267\u884c\u4e86\uff1f<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aPython\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a \u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u6267\u884c\u7ed3\u675f\u65f6\u6253\u5370\u4e86\u591a\u4e2a\u201cThread-5\u201d\uff0c\u5374\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u771f\u6b63\u6267\u884c\u4e86\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/173244855167431127f3756.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u7ebf\u7a0b\u91cd\u590d\u6267\u884c<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u6267\u884c\u7ed3\u675f\u65f6\u6253\u5370\u4e86\u591a\u4e2a\u201cthread-5\u201d\uff0c\u770b\u8d77\u6765\u7ebf\u7a0b5\u91cd\u590d\u6267\u884c\u4e86\uff1f\u53bb\u6389sub()\u51fd\u6570\u4e2d\u7684time.sleep(1)\u540e\uff0c\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u53c8\u53ef\u4ee5\u6b63\u5e38\u6267\u884c\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u6309\u987a\u5e8f\u6267\u884c\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p>\u7a0b\u5e8f\u4e2d\u91cd\u590d\u6267\u884csub()\u51fd\u6570\u7684\u7ebf\u7a0b\u662f\u7ebf\u7a0b5\u7684\u539f\u56e0\u5728\u4e8e\uff1a<\/p>\n<ul>\n<li>for\u5faa\u73af\u521b\u5efa\u4e865\u4e2a\u7ebf\u7a0b\uff0c\u5176\u4e2d\u6700\u540e\u4e00\u4e2a\u7ebf\u7a0b\u7684\u540d\u79f0\u4e3a\u201cthread-5\u201d\u3002<\/li>\n<li>sub()\u51fd\u6570\u4f7f\u7528\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cft\uff0c \u8be5\u53d8\u91cf\u5b58\u50a8\u4e86\u5f53\u524d\u6b63\u5728\u6267\u884csub()\u51fd\u6570\u7684\u7ebf\u7a0b\u3002<\/li>\n<li>\u5728sub()\u51fd\u6570\u4e2d\uff0ct\u5168\u5c40\u53d8\u91cf\u59cb\u7ec8\u662f\u7ebf\u7a0b5\uff0c\u56e0\u4e3a\u5faa\u73af5\u6b21\u540e\uff0ct\u53d8\u91cf\u7684\u503c\u6c38\u8fdc\u6307\u5411\u201cthread-5\u201d\u7ebf\u7a0b\u3002\u56e0\u6b64\uff0c\u6240\u6709\u7ebf\u7a0b\u90fd\u6253\u5370\u201cthread-5\u201d\u3002<\/li>\n<\/ul>\n<p>\u53bb\u9664time.sleep(1)\u540e\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u6309\u987a\u5e8f\u6267\u884c\uff0c\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u7531\u4e8etime.sleep(1)\u7684\u79fb\u9664\uff0c\u7ebf\u7a0b5\u4e0d\u4f1a\u88ab\u963b\u585e\uff0c\u4ece\u800c\u5141\u8bb8\u5176\u4ed6\u7ebf\u7a0b\u6709\u673a\u4f1a\u6267\u884c\u5e76\u66f4\u65b0t\u5168\u5c40\u53d8\u91cf\u3002<\/li>\n<li>\u6bcf\u4e2a\u7ebf\u7a0b now\u90fd\u66f4\u65b0\u4e86t\u5168\u5c40\u53d8\u91cf\uff0c\u6307\u5411\u6b63\u786e\u7684\u7ebf\u7a0b\u540d\u79f0\uff0c\u4ece\u800c\u6b63\u786e\u6253\u5370\u7ebf\u7a0b\u540d\u79f0\u3002<\/li>\n<\/ul>\n<p><strong>\u6b63\u786e\u7684\u7a0b\u5e8f\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import threading, time\n\nnum = 5\nlock = threading.Lock()     # \u521b\u5efa\u540c\u6b65\u9501\n# L = []\n\n\ndef sub():\n    global num\n    print('sub %s' % t.name)\n    lock.acquire()      # \u83b7\u5f97\u540c\u6b65\u9501\uff1a\u4e0d\u8ba9\u522b\u7684\u7ebf\u7a0b\u5728\u540c\u4e00\u65f6\u523b\u8fd0\u884c\n    print(threading.current_thread().getName())\n    temp = num\n    # time.sleep(1)\n    num = temp - 1\n    lock.release()      #\u89e3\u9664\u540c\u6b65\u9501\n    print('%s ' % t.name, num)\n\n\nfor i in range(5):\n    t = threading.Thread(target=sub)\n    t.start()\n    print('start %s' % t.name)\n    # L.append(t)\n\n# for T in L:\n#     T.join()\n\nprint('s', num)<\/pre>\n<p>\u6b64\u4ee3\u7801\u786e\u4fdd\u6bcf\u4e2a\u7ebf\u7a0b\u6309\u987a\u5e8f\u6253\u5370\u5176\u540d\u79f0\u548c\u66f4\u65b0\u7684num\u503c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPython\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a \u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u6267\u884c\u7ed3\u675f\u65f6\u6253\u5370\u4e86\u591a\u4e2a\u201cThread-5\u201d\uff0c\u5374\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u771f\u6b63\u6267\u884c\u4e86\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205151","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205151"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205151\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}