python带我起飞第10章

进程:应用程序的实例。
import threading
import time
print(threading.current_thread())
def handle(sid):
  print("Thread %d run"%sid,threading.current_thread())
for i in range(1,11):
  t=threading.Thread(target=handle,args=(i,))
  t.start();
print("main thread",threading.current_thread())
class MyThread(threading.Thread):
  def __init__(self,sid):
    threading.Thread.__init__(self)
    self.sid=sid
  def run(self):
      handle(self.sid)
for i in range(1,11):
  a=MyThread(i)
  a.start()



互斥锁研究
import threading
import time
g_num=1
def handle(arg):
  global g_num
  g_num*=2
  time.sleep(arg%2)
  print ("thread",arg,":",g_num)
threads=[]
for i in range(1,11):
  t=threading.Thread(target=handle,args=(i,))
  t.start()
  threads.append(t)
for t in threads:
  t.join()
print ('main thread end')








全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务