我写了这个简单的程序来创建一个线程并运行一个方法:
import threading
class MyThread(threading.Thread):
def __init__(self, thread_name, counter):
threading.Thread.__init__(self)
self.thread_name = thread_name
self.counter = counter
def printIteration(self, counter):
it = 1
while counter > 0:
print "\n"+self.thread_name+": Iteration", it,"\n"
it += 1
counter -= 1
def run(self):
print "\n"+self.thread_name+" started...\n"
self.printIteration(self.counter)
exit()
my_thread1 = MyThread("Thread1", 2)
my_thread2 = MyThread("Thread2", 4)
my_thread1.start()
my_thread2.start()
但是,当我运行它时,输出类似于:
Thread1 started...
Thread2 started...
Thread1: Iteration
Thread2: Iteration 1 1
.
.
.
为什么呢?这是正常的还是我忘记了什么?