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()
  • 相关阅读:
    C++右值引用的参考
    U3D 文档 GPU INSTANCING
    UNITY statistic中的 SetPass和Batches
    时间复杂度
    转,数组遍历的三种方式
    bug纪录:PhotonServer-14052: 17:14:09.033
    关于.net standard 与 .net core, net framework
    【转】未能加载文件或程序集或它的某一个依赖项,系统找不到指定的文件
    C# 计时函数精度测试
    一张图看懂dex
  • 原文地址:https://www.cnblogs.com/qtnt/p/12489207.html
Copyright © 2011-2022 走看看