多线程复习1

1.进程在运行时会创建一个主线程,每一个进程只有一个主线程
2.子进程 pid 唯一标识符
3.任意时间里,只有一个线程在运行,且运行顺序不能确定(全局锁限制)
4.threading.Thread(target = test,args = [i])
    target = 函数名 ,args = [ 参数 ]
5.可以继承 threading.Thread 然后重写 run 方法
    class myThread(threading.Thread):
        def run():
            pass

程序:
# import threading 
# # 导入线程库
# def test():
#     print(1)

# a = threading.Thread(target = test)
# # 创建 a 线程
# b = threading.Thread(target = test)
# a.start()
# # 启动线程
# b.start()

# a.join()
# b.join()
# # 结束之前使用 join 等待其他线程


import threading 
# 导入线程库
import time 
def test(i):
    time.sleep(0.1)
    print(i)

tests_thread = []
for i in range(0,10):
    threads = threading.Thread(target = test,args = [i])
    # 创建 a 线程
    tests_thread.append(threads)


for i in tests_thread:
    i.start()
    # 启动线程


for i in tests_thread:
    i.join()
    # 结束之前使用 join 等待其他线程

print("线程结束")

2020-04-12

猜你喜欢

转载自www.cnblogs.com/hany-postq473111315/p/12685980.html