日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python线程任务run_Python线程类| 带有示例的run()方法

發布時間:2025/3/11 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python线程任务run_Python线程类| 带有示例的run()方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python線程任務run

Python Thread.run()方法 (Python Thread.run() Method)

Thread.run() method is an inbuilt method of the Thread class of the threading module in Python. This method is used to represent a thread's activity. It calls the method expressed as the target argument in the Thread object along with the positional and keyword arguments taken from the args and kwargs arguments, respectively. This method can also be overridden in the subclass.

Thread.run()方法是Python中線程模塊的Thread類的內置方法。 此方法用于表示線程的活動。 它調用在Thread對象中表示為目標參數的方法,以及分別從args和kwargs參數獲取的position和關鍵字參數。 也可以在子類中重寫此方法。

Module:

模塊:

from threading import Thread

Syntax:

句法:

run()

Parameter(s):

參數:

  • None

    沒有

Return value:

返回值:

The return type of this method is <class 'NoneType'>, it returns nothing.

此方法的返回類型為<class'NoneType'> ,它什么也不返回。

Example:

例:

# Python program to explain the # use of run() method in Thread classimport threadingdef thread_1(i):print('Value by Thread 1:', i)def thread_2(i):print('Value by Thread 2:', i)def thread_3(i):print('Value by Thread 3:', i) # Creating three sample threads thread1 = threading.Thread(target=thread_1, args=(1,)) thread2 = threading.Thread(target=thread_2, args=(2,)) thread3 = threading.Thread(target=thread_3, args=(3,))# Running three thread object thread1.run() thread2.run() thread3.run()

Output

輸出量

Value by Thread 1: 1 Value by Thread 2: 2 Value by Thread 3: 3

run() method can also be overridden in the subclass. Given below creates a subclass of the Thread class and overrides the run function.

run()方法也可以在子類中重寫。 下面給出的創建Thread類的子類并覆蓋run函數。

Example:

例:

# Python program to demonstrate # the overriding of run() method import threading class mythread(threading.Thread): def __init__(self, thread_name, thread_ID): threading.Thread.__init__(self) self.thread_name = thread_name self.thread_ID = thread_ID # Overrriding of run() method in the subclass def run(self): print("Thread name: "+str(self.thread_name) +" "+ "Thread id: "+str(self.thread_ID)); thread1 = mythread("thread1", 1) thread2 = mythread("thread2", 2); thread1.start() thread2.start()

Output

輸出量

Thread name: thread1 Thread id: 1 Thread name: thread2 Thread id: 2

翻譯自: https://www.includehelp.com/python/thread-run-method-with-example.aspx

python線程任務run

總結

以上是生活随笔為你收集整理的python线程任务run_Python线程类| 带有示例的run()方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。