zoukankan      html  css  js  c++  java
  • day33-python之多线程

    1.多线程实例

    # import threading
    # import time
    #
    #
    import  threading
    import time
    class MyThread(threading.Thread):
        def __init__(self,num):
            threading.Thread.__init__(self)
            self.num = num
    
        def run(self):
            print("running on number:%s"%self.num)
            time.sleep(3)
    
    if __name__ == '__main__':
        t1 = MyThread(1)
        t2 = MyThread(2)
    
        t1.start()
        t2.start()
        print("ending")
    # class MyThread(threading.Thread):
    #     def __init__(self, num):
    #         threading.Thread.__init__(self)
    #         self.num = num
    #
    #     def run(self):  # 定义每个线程要运行的函数
    #
    #         print("running on number:%s" % self.num)
    #
    #         time.sleep(3)
    #
    #
    # if __name__ == '__main__':
    #     t1 = MyThread(1)
    #     t2 = MyThread(2)
    #     t1.start()
    #     t2.start()
    #
    #     print("ending......")

    2.join

    # import threading
    # import time
    #
    #
    # def music():
    #     print("begin to listen %s"%time.ctime())
    #     time.sleep(3)
    #     print("stop to listen %s" % time.ctime())
    #
    #
    # def game():
    #     time.sleep(4)
    #     t3=threading.Thread(target=music)
    #     t3.start()
    #
    #     print("begin to play game %s"%time.ctime())
    #     time.sleep(5)
    #     print("stop to play game %s" % time.ctime())
    #
    
    #
    # if __name__ == '__main__':
    #
    #     t1=  threading.Thread(target=music)
    #
    #     t2 = threading.Thread(target=game)
    #
    #     t1.start()
    #     t2.start()
    #
    #     t1.join()
    #     t2.join()
    #
    #     print("ending")
    #
    
    
    # import threading
    # from time import ctime,sleep
    # import time
    #
    # def ListenMusic(name):
    #
    #         print ("Begin listening to %s. %s" %(name,ctime()))
    #         sleep(3)
    #         print("end listening %s"%ctime())
    #
    
    # def RecordBlog(title):
    #
    #         print ("Begin recording the %s! %s" %(title,ctime()))
    #         sleep(5)
    #         print('end recording %s'%ctime())
    #
    # threads = []
    #
    # t1 = threading.Thread(target=ListenMusic,args=('水手',))
    # t2 = threading.Thread(target=RecordBlog,args=('python线程',))
    #
    # threads.append(t1)
    # threads.append(t2)
    
    # if __name__ == '__main__':
    #     #t1.setDaemon(True)
    #     t2.setDaemon(True)
    #
    #     for t in threads:
    #         #t.setDaemon(True) #注意:一定在start之前设置
    #         t.start()
    #         print(t.getName())
    #         print("count:",threading.active_count())
    #         #t.join()#串行
    #     #t.join()
    #
    #     #t1.join()
    #     #t1.setDaemon(True)
    #
    #     #t2.join()########考虑这三种join位置下的结果?
    #
    #     while threading.active_count()==1:
    #
    #         print ("all over %s" %ctime())
    
    
    # 调用方式2:#######################################
    
    # import threading
    # import time
    #
    
    import threading
    import time
    class MyThread(threading.Thread):
        def __init__(self,num):
            threading.Thread.__init__(self)
            self.num = num
    
        def run(self):
            print("running on number:%s"%self.num)
            time.sleep(3)
    
    if __name__ == '__main__':
        t1 = MyThread(1)
        t2 = MyThread(2)
        t1.start()
        t2.start()
        print("ending")
    # class MyThread(threading.Thread):
    #
    #     def __init__(self, num):
    #         threading.Thread.__init__(self)
    #         self.num = num
    #
    #     def run(self):  # 定义每个线程要运行的函数
    #
    #         print("running on number:%s" % self.num)
    #
    #         time.sleep(3)
    #
    # if __name__ == '__main__':
    #
    #     t1 = MyThread(1)
    #     t2 = MyThread(2)
    #     t1.start()
    #     t2.start()
    #     print("ending......")
  • 相关阅读:
    Simple DirectMedia Layer常用API总结
    [游戏复刻] Super Mario Brothers(1985. Famicom)
    [游戏复刻] 2048(2014. Android)
    图的结构以及寻路算法的c实现
    散列查找的C实现
    【游戏编程从0开始】一、基本结构
    C++中const关键字用法总结
    C标准库常用函数概要
    字符串表达式计算器的设计
    初探数据结构
  • 原文地址:https://www.cnblogs.com/sqy-yyr/p/11436749.html
Copyright © 2011-2022 走看看