风-fmgao

导航

开启线程方式

#!/usr/bin/env python
# encoding: utf-8  
# Date: 2018/6/18

# # 方式一
# from threading import Thread
# import time
# import random
#
#
# def task(name):
#     print('%s is running' % name)
#     time.sleep(random.randrange(1, 5))
#     print('%s is done' % name)
#
#
# if __name__ == '__main__':
#     p = Thread(target=task, args=('子线程1',))
#     p.start()
#     print('主线程')


# 方式二
import time
import random
from threading import Thread


class MyThread(Thread):
    def __init__(self, name):
        super().__init__()
        self.name = name

    def run(self):
        print('%s is running' % self.name)
        time.sleep(random.randrange(1,5))
        print('%s is done' % self.name)


if __name__ == '__main__':
    t1 = MyThread('线程1')
    t1.start()
    print('主线程')


posted on 2018-06-18 08:49  风-fmgao  阅读(107)  评论(0编辑  收藏  举报