import threading
def task1(): print(“线程1”)
def task2(): print(“线程2”)
def main(): # threading.Thread创建线程 thread1 = threading.Thread(target=task2) # 启动线程 thread1.start() thread2 = threading.Thread(target=task1) thread2.start() print(“线程3”) if name == ‘main’: main()
import threading import time
def task1(): time.sleep(5)
def main(): start_time = time.time() # threading.Thread创建线程 thread1 = threading.Thread(target=task1) # 启动线程 thread1.start() thread2 = threading.Thread(target=task1) thread2.start() # 让其他线程等待自己执行完成thread1.join() thread1.join() thread2.join() end_time=time.time()
print(end_time-start_time)
if name == ‘main’: main()
|