1. 程式人生 > >Python多執行緒程式設計

Python多執行緒程式設計

#!/usr/bin/python 
#!coding=utf-8
import threading
import time
exitFlag = 0
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
): task() def task(self): '''執行緒要執行的任務''' print 'starting ' + self.name print 'exiting ' + self.name #建立執行緒 thread1 = MyThread(1,'Thread-1', 1) thread2 = MyThread(2,'Thread-2', 2) #啟動執行緒 thread1.start() thread2.start() #等待執行緒結束 thread1.join() thread2.join() print
'exiting main thread'