zoukankan      html  css  js  c++  java
  • 多线程同步引入锁

    # _*_ coding: utf-8 _*_
    __author__ = 'pythonwu'
    __date__ = "2018/6/20 17:42"

    import threading
    import time

    exitFlag = 10
    class myThread(threading.Thread):
    def __init__(self,threadID,name,counter):
    threading.Thread.__init__(self) #重写父类的__init__方法
    self.threadID = threadID
    self.name = name
    self.counter = counter

    def run(self):
    print("Starting " + self.name)
    threadLock.acquire() #获得锁
    print_time(self.name,self.counter,3) #执行程序
    threadLock.release() #释放锁


    def print_time(threadName,delay,counter):
    while counter:
    global exitFlag
    time.sleep(2)
    print("%s:%s" %(threadName,time.ctime(time.time())))
    counter -= 1
    exitFlag -= 1
    print(exitFlag)

    threadLock = threading.Lock()
    threads = []

    thread1 = myThread(1,"Thread-1",1)
    thread2 = myThread(2,"Thread-2",2)

    thread1.start()
    thread2.start()

    threads.append(thread1)
    threads.append(thread2)

    for t in threads:
    t.join()
    print("Exiting Main Thread")




  • 相关阅读:
    mui 页面跳转
    mui 下拉刷新
    mui 上拉加载更多
    mui 页面传值
    mui 监听app运行状态
    mui webview操作
    mui ajax方法
    ionic 图片加载失败,显示默认图片代替
    mui 侧滑菜单
    ionic中关于ionicView 的生命周期
  • 原文地址:https://www.cnblogs.com/wudeng/p/9205137.html
Copyright © 2011-2022 走看看