1. 程式人生 > >使用兩種方式編寫多執行緒程式?

使用兩種方式編寫多執行緒程式?

# 方案1
from threading import Thread
import time

class Sayhi(Thread):
    def __init__(self,name):
        super().__init__()
        self.name=name
    def run(self):
        time.sleep(2)
        print('%s say hello' % self.name)

if __name__ == '__main__':
    t = Sayhi('妹子')
    t.start()
    
print('主執行緒')
# 方案2
from threading import Thread
import time
def sayhi(name):
    time.sleep(2)
    print('%s say hello' %name)

if __name__ == '__main__':
    t=Thread(target=sayhi,args=('apollo',))
    t.start()
    print('主執行緒')