zoukankan      html  css  js  c++  java
  • 多线程加锁并发任务

    # coding=utf-8
    import threading
    import time
    import string
    import random
    
    
    class TestTreading(threading.Thread):
        def __init__(self,func):
            threading.Thread.__init__(self)
            self.func =func
        #@override
        def run(self):
            mutex = threading.Lock()
            mutexFlage = mutex.acquire()
            if mutexFlage:
                self.res=self.func()
                print(time.asctime())
            mutex.release()
            return self.res
        # def get_res(self):
        #     return  self.res
    def add():
        return ''.join(random.sample(string.hexdigits,random.randint(16,18)))
    
    def executors():
        resList=[]
        pool=[]
        for i in range(10000):
            th=TestTreading(add)
            pool.append(th)
    
        for t in pool:
            t.start()
        for t in pool:
            t.join()
            resList.append(t.run())
        return resList
    if __name__ == '__main__':
        executors()
    

      

  • 相关阅读:
    百度之星初赛 A
    百度之星 初赛 BC
    2016 百度之星资格赛
    codeforces 749
    codeforces 785
    HDU 4617
    网络流 poj 2195
    网络流 poj 3436 poj 3281
    codeforces 780 C
    idea激活
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/10631541.html
Copyright © 2011-2022 走看看