Python 多线程简单例子

来源:互联网 发布:淘宝助理怎么下载 编辑:程序博客网 时间:2024/05/22 14:15
Python代码 
  1. import threading  
  2. import time  
  3.   
  4. class MyThread(threading.Thread):  
  5.     def __init__(self, threadnum, max):  
  6.         threading.Thread.__init__(self)  
  7.           
  8.         self.threadnum = threadnum  
  9.           
  10.         if (max < 0 ):  
  11.             self.max = 0  
  12.         else:  
  13.             self.max = max  
  14.     def run(self):  
  15.         for x in xrange(self.max):  
  16.             print "thread-%d %d" % (self.threadnum, x)  
  17.             time.sleep(0.1);#sleep 100ms  
  18.   
  19. MyThread(110).start();#start the 1st thread  
  20. MyThread(210).start();#start the 2nd thread  
  21. MyThread(310).start();#start the 3rd thread  


http://ghostfromheaven.iteye.com/admin/blogs/1291083