python多线程id获取
demo
import threading
import time
def print_thread_info(thread_name):
"""线程函数,打印线程名称和ID以及一些文本"""
for i in range(3):
time.sleep(1)
thread_id = threading.current_thread().ident
print(f"{thread_name} (ID: {thread_id}): 这是第 {i+1} 次打印")
# 创建线程列表
threads = []
# 创建并启动线程
for i in range(5):
thread = threading.Thread(target=print_thread_info, args=(f"线程{i+1}",))
thread.start()
threads.append(thread)
# 等待所有线程完成
for thread in threads:
thread.join()
print("所有线程已完成.")
output
本文来自博客园,作者:__username,转载请注明原文链接:https://www.cnblogs.com/code3/p/17983978