{"id":751,"date":"2019-05-01T17:58:00","date_gmt":"2019-05-01T09:58:00","guid":{"rendered":"http:\/\/www.eait.co\/?p=751"},"modified":"2019-05-01T18:00:15","modified_gmt":"2019-05-01T10:00:15","slug":"%e7%ba%bf%e7%a8%8b%e4%b8%8e%e7%ba%bf%e7%a8%8b%e6%b1%a0","status":"publish","type":"post","link":"https:\/\/notes.coremix.net\/?p=751","title":{"rendered":"\u7ebf\u7a0b\u4e0e\u7ebf\u7a0b\u6c60"},"content":{"rendered":"<div>import time<\/div>\n<div>from threading import Thread<\/div>\n<div><\/div>\n<div>def func():<\/div>\n<div>print(&#8216;start&#8217;)<\/div>\n<div>time.sleep(2) #\u5ef6\u65f6 \u6a21\u62df\u767e\u5ea6\u53d1\u51fa\u8bf7\u6c42<\/div>\n<div>print(&#8216;end&#8217;)<\/div>\n<div><\/div>\n<div>if __name__==&#8217;__main__&#8217;:<\/div>\n<div># t = Thread(target=func) #\u521b\u5efa\u51fa\u6765\u7684\u5b50\u7ebf\u7a0b<\/div>\n<div># t.start()<\/div>\n<div># print(&#8216;\u4e3b\u7ebf\u7a0b\u7ed3\u675f&#8217;)<\/div>\n<div><\/div>\n<div>t_list=[]<\/div>\n<div>for i in range(100):<\/div>\n<div>t = Thread(target=func)<\/div>\n<div>t_list.append(t)<\/div>\n<div># t.start()<\/div>\n<div>#t.join()\u5199\u5230start\u540e\u9762\u5c31\u53d8\u6210\u4e86\u4e00\u4e2a\u4e00\u4e2a\u6267\u884c\u4e86(join\u7684\u4f5c\u7528\u662f\u7b49\u5f85\u8c03\u7528join\u7684\u5b50\u73b0\u573a\u7ed3\u675f)<\/div>\n<div>for t in t_list:<\/div>\n<div>t.start()<\/div>\n<div>for ti in t_list:<\/div>\n<div>t.join()<\/div>\n<div><\/div>\n<div>print(&#8216;\u4e3b\u7ebf\u7a0b\u7ed3\u675f&#8217;)<\/div>\n<div><\/div>\n<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/div>\n<div>from threading import Thread<\/div>\n<div>import time<\/div>\n<div><\/div>\n<div>class my_thread(Thread):<\/div>\n<div>def __init__(self):<\/div>\n<div>super(my_thread, self).__init__()<\/div>\n<div>def run(self):# \u5fc5\u987b\u8981\u5199 \u8fd9\u662f\u7ebf\u7a0b\u8981\u6267\u884c\u7684\u5185\u5bb9<\/div>\n<div>print(&#8216;11111&#8217;)<\/div>\n<div>time.sleep(2)<\/div>\n<div>print(&#8216;2222&#8217;)<\/div>\n<div><\/div>\n<div>if __name__ ==&#8217;__main__&#8217;:<\/div>\n<div>for i in range(10):<\/div>\n<div>t = my_thread()<\/div>\n<div>t.setDaemon(True)#\u8bbe\u7f6e\u5b88\u62a4\u7ebf\u7a0b,\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u8be5\u5b50\u7ebf\u7a0b\u4e5f\u4f1a\u7ed3\u675f<\/div>\n<div>t.start() #\u8c03\u7528\u7ebf\u7a0b<\/div>\n<div>print(&#8216;\u4e3b\u7ebf\u7a0b\u7ed3\u675f&#8217;)<\/div>\n<div><\/div>\n<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/div>\n<div>from threading import Thread,Lock<\/div>\n<div><\/div>\n<div>x = 0<\/div>\n<div>n = 100000<\/div>\n<div>lock = Lock()<\/div>\n<div><\/div>\n<div>def incr(n):<\/div>\n<div>global x<\/div>\n<div>for i in range(n):<\/div>\n<div>lock.acquire()<\/div>\n<div>x +=1<\/div>\n<div>lock.release()<\/div>\n<div>def dncr(n):<\/div>\n<div>global x<\/div>\n<div>for i in range(n):<\/div>\n<div>lock.acquire()<\/div>\n<div>x -=1<\/div>\n<div>lock.release()<\/div>\n<div>t1 = Thread(target=incr,args=(n,))<\/div>\n<div>t2 = Thread(target=dncr,args=(n,))<\/div>\n<div>t1.start()<\/div>\n<div>t2.start()<\/div>\n<div>t1.join()#\u4e0d\u52a0\u8fd9\u4e24\u4e2ajoin\u4efb\u7136\u65e0\u6cd5\u6253\u5370\u51fa0,\u56e0\u4e3a\u7ebf\u7a0b\u672a\u7ed3\u675f\u5c31print(x)\u4e86<\/div>\n<div>t2.join()<\/div>\n<div>print(x)<\/div>\n<div><\/div>\n<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/div>\n<div>\u961f\u5217<\/div>\n<div>import queue<\/div>\n<div>from threading import Thread<\/div>\n<div>import random<\/div>\n<div><\/div>\n<div>q = queue.Queue(5) #\u5b9e\u4f8b<\/div>\n<div>def incr(q):<\/div>\n<div>for i in range(20):<\/div>\n<div>num = random.randint(1,20)<\/div>\n<div>q.put(num)<\/div>\n<div>print(&#8216;\u6dfb\u52a0\u7684\u6570\u636e\u4e3a{},\u5f53\u524d\u961f\u5217\u7684\u957f\u5ea6\u4e3a{}&#8217;.format(num,q.qsize()))<\/div>\n<div>def dncr(q):<\/div>\n<div>for i in range(20):<\/div>\n<div>num = q.get()<\/div>\n<div>print(&#8216;\u83b7\u53d6\u7684\u6570\u636e\u4e3a{},\u5f53\u524d\u961f\u5217\u7684\u957f\u5ea6\u4e3a{}&#8217;.format(num,q.qsize()))<\/div>\n<div>t1 = Thread(target = incr,args=(q,))<\/div>\n<div>t2 = Thread(target = dncr,args=(q,))<\/div>\n<div>t1.start()<\/div>\n<div>t2.start()<\/div>\n<div>t1.join()<\/div>\n<div>t2.join()<\/div>\n<div><\/div>\n<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>import time from threading import Thread def func(): pr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,23],"tags":[],"class_list":["post-751","post","type-post","status-publish","format-standard","hentry","category-java","category-python_note"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=751"}],"version-history":[{"count":1,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/751\/revisions"}],"predecessor-version":[{"id":752,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/751\/revisions\/752"}],"wp:attachment":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}