python import threading class MyThread(threading.Thread): def __init__(self, name): threading.Thread.__init__(self) self.name = name def run(self): print("Task started by thread:", self.name) print("Task completed by thread:", self.name) thread1 = MyThread("Thread 1") thread2 = MyThread("Thread 2") thread1.start() thread2.start() thread1.join() thread2.join() print("All tasks completed") python import concurrent.futures def task(name): print("Task started by thread:", name) print("Task completed by thread:", name) with concurrent.futures.ThreadPoolExecutor() as executor: executor.submit(task, "Thread 1") executor.submit(task, "Thread 2") print("All tasks completed")


上一篇:
下一篇:
切换中文