zoukankan      html  css  js  c++  java
  • 线程event事件函数实现红绿灯

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # author aliex-hrg
    
    import threading,time
    
    event = threading.Event()
    
    def redlight():
        event.set()
        count = 1
        while True:
            if count > 0 and count < 6:
                event.set()
                print("33[44;1mGreen light...{}33[0m".format(6 - count))
            elif count < 10:
                event.clear()
                print("33[41;1mRed light...{}33[0m".format(11 - count))
            else:
                print("33[41;1mRed light...{}33[0m".format(11 - count))
                count = 0
            count += 1
            time.sleep(1)
    def car(name):
        while True:
            if event.is_set():
                print("33[34;1m%s is running...33[0m" %name)
            else:
                print("33[31;1m%s is stop...33[0m" % name)
                event.wait()     #程序走到这儿,如果event没有被set,会一直等待,直到被set了才会继续向下运行
            time.sleep(1)
    
    run = threading.Thread(target=redlight)
    run.start()
    car1 = threading.Thread(target=car,args=("car1",))
    car1.start()
    car2 = threading.Thread(target=car,args=("car2",))
    car2.start()
    car3 = threading.Thread(target=car,args=("car3",))
    car3.start()
    

      

  • 相关阅读:
    luogu P3804 【模板】后缀自动机 (SAM)
    莫队
    luogu P4688 [Ynoi2016]掉进兔子洞
    FZOJ 2331 LYK loves graph
    字典树
    luogu P6623 [省选联考 2020 A 卷] 树
    luogu P6018 [Ynoi2010]Fusion tree
    luogu P3264 [JLOI2015]管道连接
    最小斯坦纳树
    9. 回文数
  • 原文地址:https://www.cnblogs.com/alex-hrg/p/9019732.html
Copyright © 2011-2022 走看看