zoukankan      html  css  js  c++  java
  • 多线程

    多线程

     1 import threading
     2 import time
     3 
     4 
     5 def music():
     6     print('begin to listen music %s' % time.ctime())
     7     time.sleep(3)
     8     print('stop to listen music %s' % time.ctime())
     9 
    10 
    11 def game():
    12     print('begin to play game%s' % time.ctime())
    13     time.sleep(5)
    14     print('stop to play game%s' % time.ctime())
    15 
    16 
    17 if __name__ == '__main__':
    18     t1 = threading.Thread(target=music)
    19     t1.start()
    20     t2 = threading.Thread(target=game)
    21     t2.start()
    22     t2.join()  # 等待子线程结束
    23     print('ending')
    24 结果
    25 begin to listen music Sat Apr 11 23:01:55 2020
    26 begin to play gameSat Apr 11 23:01:55 2020
    27 stop to listen music Sat Apr 11 23:01:58 2020
    28 stop to play gameSat Apr 11 23:02:00 2020
    29 ending
    30 
    31 进程完成,退出码 0

    设置守护线程,可以让某个子线程跟随子线程结束而结束

     1 import threading
     2 import time
     3 
     4 
     5 def music():
     6     print('begin to listen music %s' % time.ctime())
     7     time.sleep(3)
     8     print('stop to listen music %s' % time.ctime())
     9 
    10 
    11 def game():
    12     print('begin to play game%s' % time.ctime())
    13     time.sleep(5)
    14     print('stop to play game%s' % time.ctime())
    15 
    16 
    17 l = list()
    18 
    19 t1 = threading.Thread(target=music)
    20 
    21 t2 = threading.Thread(target=game)
    22 t2.setDaemon(True)
    23 l.append(t1)
    24 l.append(t2)
    25 输出:
    26 begin to listen music Sat Apr 11 23:17:56 2020
    27 begin to play gameSat Apr 11 23:17:56 2020
    28 ending
    29 stop to listen music Sat Apr 11 23:17:59 2020
    30 
    31 进程完成,退出码 0

     1 import threading
     2 import time
     3 
     4 
     5 def music():
     6     print('begin to listen music %s' % time.ctime())
     7     time.sleep(3)
     8     print('stop to listen music %s' % time.ctime())
     9 
    10 
    11 def game():
    12     print('begin to play game%s' % time.ctime())
    13     time.sleep(5)
    14     print('stop to play game%s' % time.ctime())
    15 
    16 
    17 l = list()
    18 
    19 t1 = threading.Thread(target=music)
    20 
    21 t2 = threading.Thread(target=game)
    22 t2.setDaemon(True)
    23 l.append(t1)
    24 l.append(t2)
    25 if __name__ == '__main__':
    26     for t in l:
    27         # t.setDaemon(True)  # 守护线程,使该线程跟着主线程一起结束
    28         t.start()
    29         # print(threading.active_count())
    30         print(t.getName())
    31         print(threading.current_thread())
    32     print(threading.active_count())
    33     print(threading.enumerate())
    34     print(threading.current_thread())
    35     print('ending')
    36 输出:
    37 begin to listen music Sat Apr 11 23:32:12 2020Thread-1
    38 <_MainThread(MainThread, started 9716)>
    39 
    40 begin to play gameSat Apr 11 23:32:12 2020
    41 Thread-2
    42 <_MainThread(MainThread, started 9716)>
    43 3
    44 [<_MainThread(MainThread, started 9716)>, <Thread(Thread-1, started 16748)>, <Thread(Thread-2, started daemon 14432)>]
    45 <_MainThread(MainThread, started 9716)>
    46 ending
    47 stop to listen music Sat Apr 11 23:32:15 2020
    48 
    49 进程完成,退出码 0
  • 相关阅读:
    2017-2018-2 20179223《网络攻防技术》第七周作业
    2017-2018-2 20179223《网络攻防技术》第六周作业
    2017-2018-2 20179223《网络攻防技术》第五周作业
    2017-2018-2 20179223《密码与安全新技术》第二周作业
    2017-2018-2 20179223《网络攻防技术》第四周作业
    2017-2018-2 20179223《密码与安全新技术》第一周作业
    2017-2018-2 20179223《网络攻防技术》第三周作业
    2018-2018-2 《网络攻防》第二周作业
    51nod1270 数组的最大代价(简单dp)
    51nod1269 B君的圆锥
  • 原文地址:https://www.cnblogs.com/ch2020/p/12683126.html
Copyright © 2011-2022 走看看