zoukankan      html  css  js  c++  java
  • threading

    import threading
    import time

    def run(n):
    print("task ",n )
    time.sleep(2)
    print("task done",n)

    start_time = time.time()
    t_objs = [] #存线程实例
    for i in range(50):
    t = threading.Thread(target=run,args=("t-%s" %i ,))
    t.start()
    t_objs.append(t) #为了不阻塞后面线程的启动,不在这里join,先放到一个列表里

    # for t in t_objs: #循环线程实例列表,等待所有线程执行完毕
    # t.join()


    print("----------all threads has finished...")
    print("cost:",time.time() - start_time)
    # run("t1")
    # run("t2")

    #===================

    import threading
    import time

    class MyThread(threading.Thread):
    def __init__(self,n,sleep_time):
    super(MyThread,self).__init__()
    self.n = n
    self.sleep_time = sleep_time
    def run(self):
    print("runnint task ",self.n )
    time.sleep(self.sleep_time)
    print("task done,",self.n )


    t1 = MyThread("t1",2)
    t2 = MyThread("t2",4)

    t1.start()
    t2.start()

    t1.join() #=wait()
    t2.join()

    print("main thread....")


    #============================

    import threading
    import time

    def run(n):
    print("task ",n )
    time.sleep(2)
    print("task done",n,threading.current_thread())

    start_time = time.time()
    t_objs = [] #存线程实例
    for i in range(50):
    t = threading.Thread(target=run,args=("t-%s" %i ,))
    t.setDaemon(True) #把当前线程设置为守护线程
    t.start()
    t_objs.append(t) #为了不阻塞后面线程的启动,不在这里join,先放到一个列表里

    # for t in t_objs: #循环线程实例列表,等待所有线程执行完毕
    # t.join()

    time.sleep(2)
    print("----------all threads has finished...",threading.current_thread(),threading.active_count())
    print("cost:",time.time() - start_time)
    # run("t1")
    # run("t2")
  • 相关阅读:
    根据连接速度选择地址访问
    ASP.NET探针
    C#格式化成小数
    常用经典SQL语句
    比较两个DataSet,并产生增量数据
    实用JS代码大全
    写入、读取、清除Cookie的类
    Base64加密解密
    HttpModule,HttpHandler,HttpHandlerFactory简单使用
    任务栏自定义怎么删除过去项目
  • 原文地址:https://www.cnblogs.com/rongye/p/9977276.html
Copyright © 2011-2022 走看看