线程

 

https://codeleading.com/article/2672239990/

 

import threading
import time

class myThread(threading.Thread):
    def __init__(self,threadID,name,counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter

    def run(self):
        print('start thread' + self.name)
        print_time(self.name,self.counter,1)
        print('exit time'+ self.name)

def print_time(threadName,counter,delay):
    while counter:
        time.sleep(delay)
        print(threadName,time.ctime(time.time()))
        counter -= 1

if __name__ == "__main__":
    thread1 = myThread(1,'thread1',5)
    thread2 = myThread(2,'thread2',10)

    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()
    print('exit main')

 

https://www.zhihu.com/question/492028213/answer/3104145929?utm_id=0

 

import threading
​
# 继承Thread类创建线程
class MyThread(threading.Thread):
    def run(self):
        # 线程执行的代码
        print("Hello, World!")
​
        # 使用函数创建线程
def my_function():
    # 线程执行的代码
    print("Hello, World!")
​
    # 创建线程对象并启动线程
thread1 = MyThread()
thread2 = threading.Thread(target=my_function)
thread1.start()
thread2.start()

作者:子午
链接:https://www.zhihu.com/question/492028213/answer/3104145929
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

posted @ 2023-11-03 20:24  liushao-AI  阅读(3)  评论(0编辑  收藏  举报