python线程的问题

时间:2013-03-06 03:46:30

标签: python multithreading python-2.7

这个程序中的两个线程,当这个程序运行一段时间后,只有一个线程打印出它的myid,为什么?另一个人有什么问题?

from threading import Thread  
from time import sleep  

class MyThread(Thread):  
      def __init__(self,myid):  
          Thread.__init__(self)  
          self.myid = myid  
          print self.myid  
      def run(self):  
          flag = True  
          while flag:  
              try:  
                  print self.myid  
                  sleep(0.1)  
              except Exception,e:  
                  print e  
                  flag = False  


if __name__ =="__main__":  
      threads = []  
      for i in range(1,3):  
          t = MyThread(i)  
          threads.append(t)  
      for t in threads:  
          t.start()  
      threads[0].join()  
      print '1 is finished'  
      threads[1].join()  
      print '2 is finished'  
      print 'All finished'  

0 个答案:

没有答案