python 中线程

import threading
import time

class Test(threading.Thread):
    # 继承threading.Thread
    def __init___(self):
        super(Test, self).__init__()

    def run(self):
        # 设置线程方法
        threadname = threading.currentThread().getName()
        for x in xrange(10):
            print threadname, x, 
            print 
            time.sleep(1)
            

threads = []
for i in xrange(1, 5):
    threads.append(Test())

for t in threads:
    #启动线程
    t.start()

# 主线程中等待所有子线程退出
for i in threads:
    t.join() # 线程完成

# 如果不加上面t.join,将会一直等待下去,无法打印出end

print  'end'

如果你的主线程中有别的事情要做,就无需加join,加join只是告诉主线程,所有子线程已经完成
        

原文地址:https://www.cnblogs.com/itfenqing/p/4429467.html