博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

多线程简单实例

Posted on 2018-05-09 11:31  alex_hrg  阅读(147)  评论(0编辑  收藏  举报
import threading,time

#直接函数方式
# def run1(n):
#     print(n)
#     time.sleep(2)
# t1 = threading.Thread(target=run1,args=("t1",))
# t1.start()
# t2 = threading.Thread(target=run1,args=("t2",))
# t2.start()

#类方式
class MyThread(threading.Thread):
    def __init__(self,n):
        super(MyThread,self).__init__()
        self.n = n
    def run(self):      #这里函数名必须是叫run
        print("run in thread...",self.n)
        time.sleep(2)

t1 = MyThread("t1")
t2 = MyThread("t2")
t1.start()
t2.start()