def dance(): for _ in range(3): print("dancd") time.sleep(1) def sing(): for _ in range(3): print("sing") time.sleep(1) def dance(n): for _ in range(n): print("dancd") time.sleep(1) def sing(n): for _ in range(n): print("sing") time.sleep(1) def test1(): #t1=threading.Thread(target=sing) tstat=time.time() dance() sing() tend=time.time()-tstat print("耗时:",tend) def test2(): tstat = time.time() #创建多线程 t1=threading.Thread(target=sing) t2=threading.Thread(target=dance) #启动 异步执行 #t1.start() #t2.start() #启动 同步执行 花时间多,当然,看操作具体情况选择同步或异步 t1.start() t2.start() t1.join() #等待线程1,再执行第二线程 t2.join() tend=time.time()-tstat print("耗时:",tend) def test3(): tstat = time.time() #创建多线程 t1=threading.Thread(target=sing,args=(3,)) t2=threading.Thread(target=dance,args=(2,)) #启动 异步执行 #t1.start() #t2.start() #启动 同步执行 花时间多,当然,看操作具体情况选择同步或异步 t1.start() t2.start() t1.join() #等待线程1,再执行第二线程 t2.join() tend=time.time()-tstat print("耗时:",tend)
def dance(): for _ in range(3): print("dancd") time.sleep(1) def sing(): for _ in range(3): print("sing") time.sleep(1) def test1(): #t1=threading.Thread(target=sing) tstat=time.time() dance() sing() tend=time.time()-tstat print("耗时:",tend) def test2(): tstat = time.time() #创建多线程 t1=threading.Thread(target=sing) t2=threading.Thread(target=dance) #启动 异步执行 #t1.start() #t2.start() #启动 同步执行 花时间多,当然,看操作具体情况选择同步或异步 t1.start() t2.start() t1.join() #等待线程1,再执行第二线程 t2.join() tend=time.time()-tstat print("耗时:",tend)
标签:thread,python,t2,t1,start,time,print,sing From: https://www.cnblogs.com/geovindu/p/17557525.html