py07_02:多线程

 

概念:

  并行:指每个cpu独立执行一个任务

  并发:指一个或多个cpu同时轮询执行许多的任务

 线程的使用标准

注意:传参args后面必须是元组

import time
import threading


def sing():
    for i in range(5):
        time.sleep(0.5)
        print('......唱歌中')


def dance():
    for i in range(5):
        print('跳舞中....')


def main():
    t1 = threading.Thread(target=sing)
    t2 = threading.Thread(target=dance)
    t1.start()
    t2.start()
    count = 0
    while True:
        length = len(threading.enumerate())
        print('当前运行的线程数为%d' % length)
        if count == 10:
            break
        count += 1
        time.sleep(0.5)

if __name__ == '__main__':
    main()

 

 

多线程之class

注意点:

  必须继承(threading.Thread)

  必须有run方法,多线程start()操作调用的是run方法

 

 

posted on 2020-03-24 12:02  yeyu1314  阅读(131)  评论(0编辑  收藏  举报