zoukankan      html  css  js  c++  java
  • python多线程Event实现红绿灯案例

    代码:

    # __author__ = 'STEVEN'
    # coding = utf-8
    import time,threading
    #开启事件
    event = threading.Event()
    count = 0
    class Lighter(threading.Thread):
        def run(self):
            while True:
                global count
                count += 1
                time.sleep(0.4)
                event.set()
                #设置绿灯时间为10s,红灯为10s
                if count>10 and count<=20:
                    event.clear()
                    print('33[41;1m red light ..33[0m')
                elif count > 20:
                    count = 0
                    event.set()
                    print('33[42;1m green light ..33[0m')
                else:
                    print('33[42;1m green light ..33[0m')
    
    
    class Car(threading.Thread):
        def __init__(self,name):
            super(Car,self).__init__()
            self.name = name
        def run(self):
            time.sleep(0.5)
            if event.is_set():
                print('{} passed the light'.format(self.name))
            else:
                print('{} is waiting the green light'.format(self.name))
    
    l = Lighter()
    l.start()
    #启动50辆车,让他们经过红绿灯
    for i in range(50):
        time.sleep(1)
        c = Car('car{}'.format(i))
        c.start()
    

      

  • 相关阅读:
    怎么在myeclipse中怎么集成Tomcat。
    JSP .基础概念
    继承
    封装
    什么是面向对象
    数据排序
    开发的套路
    Javabean规范
    转发和重定向
    md5加密
  • 原文地址:https://www.cnblogs.com/g177w/p/8157387.html
Copyright © 2011-2022 走看看