在多线程编程上混淆输出

时间:2013-12-02 14:55:15

标签: python multithreading output python-multithreading

我写了这个简单的程序来创建一个线程并运行一个方法:

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
.
.
.

为什么呢?这是正常的还是我忘记了什么?

0 个答案:

没有答案
相关问题