zoukankan      html  css  js  c++  java
  • Python利用Event类模拟红绿灯

    #利用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...33[0m")
            elif count > 10:
                event.set()  # 绿灯,设置标志位
                count = 0
            else:
                print("33[42;1mgreen light is on...33[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()
    

      

    #利用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...33[0m")
    elif count > 10:
    event.set() # 绿灯,设置标志位
    count = 0
    else:
    print("33[42;1mgreen light is on...33[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()
  • 相关阅读:
    leetcode-237-删除链表中的节点
    leetcode-125-验证回文串
    leetcode-217-存在重复元素
    leetcode-189-旋转数组
    leetcode-121-买卖股票的最佳时机
    leetcde-27-移除元素
    redis相关
    leetcode-26-删除排序数组中的重复项
    leetcode-16-最接近的三数之和
    基础-递归
  • 原文地址:https://www.cnblogs.com/qtnt/p/12489207.html
Copyright © 2011-2022 走看看