0121 threading库 多线程练习

import threading
import time
#线程实现
def run(n):
    print("task",n)
    time.sleep(1)
    print("3s")
    time.sleep(1)
    print("2s")
    time.sleep(1)
    print("1s")
    time.sleep(1)
    print("0s")
    time.sleep(1)
def run(n):
    for i in range(1,101):
        print(n,"跑了",i,"米")

if __name__ == '__main__':
    t1 = threading.Thread(target=run,args=("刘奕铭",))
    t2 = threading.Thread(target=run, args=("付强",))
    t3 = threading.Thread(target=run, args=("孙鑫",))

    t1.start()
    t2.start()
    t3.start()

#自定义线程

class MyThread(threading.Thread):
    def __init__(self,n):
        super().__init__()
        self.n = n

    def run(self):
        for i in range(1,101):
            print(self.n,"跑了",i,"米")


if __name__ == "__main__":
    t1 = MyThread("付强")
    t2 = MyThread("孙鑫")
    t3 = MyThread("刘奕铭")
    t1.start()
    t2.start()
    t3.start()

#守护线程

def run(n):
    print("task",n)
    time.sleep(1)
    print("3s")
    time.sleep(1)
    print("2s")
    time.sleep(1)
    print("1s")
    time.sleep(1)
    print("0s")
    time.sleep(1)
def run(n):
    for i in range(1,101):
        print(n,"跑了",i,"米")

if __name__ == '__main__':
    t1 = threading.Thread(target=run,args=("刘奕铭",))
    t2 = threading.Thread(target=run, args=("付强",))
    t3 = threading.Thread(target=run, args=("孙鑫",))
    t1.setDaemon(True)
    t2.setDaemon(True)
    t3.setDaemon(True)
    t1.start()
    t2.start()
    t3.start()
    t1.join()
    t2.join()
    t3.join()
    print("end")

#多线程
piao = 100

def worker(n):
    global piao

    while piao>0:
        lock.acquire()
        piao -= 1
        lock.release()
        print(n,"买了一张票,剩余:",piao)

if __name__ == "__main__":
    lock = threading.Lock()
    t1 = threading.Thread(target=worker,args=("worker1",))
    t2 = threading.Thread(target=worker,args=("worker2",))

    t2.start()
    t1.start()
    t1.join()
    t2.join()




from threading import Thread,Lock
import os,time
def work():
    global n
    lock.acquire()
    temp=n
    time.sleep(0.1)
    n=temp-1
    lock.release()
if __name__ == '__main__':
    lock=Lock()
    n=100
    l=[]
    for i in range(100):
        p=Thread(target=work)
        l.append(p)
        p.start()
    for p in l:
        p.join()


# 利用Event类模拟红绿灯
import threading
import time

event = threading.Event()


def lighter():
    count = 0
    event.set()  # 初始值为绿灯
    while True:
        if 5 < count <= 10:
            event.clear()  # 红灯,清除标志位
            print("\33[41;1mred light is on...\033[0m")
        elif count > 10:
            event.set()  # 绿灯,设置标志位
            count = 0
        else:
            print("\33[42;1mgreen light is on...\033[0m")

        time.sleep(1)
        count += 1


def car(name):
    while True:
        if event.is_set():  # 判断是否设置了标志位
            print("[%s] running..." % name)
            time.sleep(1)
        else:
            print("[%s] sees red light,waiting..." % name)
            event.wait()
            print("[%s] green light is on,start going..." % name)


light = threading.Thread(target=lighter, )
light.start()

car = threading.Thread(target=car, args=("MINI",))
car.start()






上一篇:使用Thread类创建多线程


下一篇:某巴二进宫,电话一面