码迷,mamicode.com
首页 > 编程语言 > 详细

创建线程的另一种方法:通过类创建(28-1)

时间:2018-09-08 12:25:18      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:round   ini   %s   main   pre   创建线程   运行   __init__   class   

 能够让CPU运行起来的就是线程!

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)  # 子线程1
  t2 = MyThread(2)  # 子线程2
  
  t1.start()  # 执行
  t2.start()  # 执行

 

创建线程的另一种方法:通过类创建(28-1)

标签:round   ini   %s   main   pre   创建线程   运行   __init__   class   

原文地址:https://www.cnblogs.com/uncle-kay/p/9608778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!