1. 程式人生 > >創建線程的另一種方法:通過類創建(28-1)

創建線程的另一種方法:通過類創建(28-1)

就是 art back threading sel print self. 運行 col

能夠讓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)