Chinaunix首页 | 论坛 | 博客
  • 博客访问: 276033
  • 博文数量: 103
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-02 16:15
文章分类

全部博文(103)

文章存档

2014年(8)

2013年(95)

我的朋友

分类: Python/Ruby

2013-12-18 23:35:26

import threading
import time,random
class ThreadLocal(threading.Thread):
        def __init__(self):
                threading.Thread.__init__(self)

                self.local = threading.local()

        def run(self):
                time.sleep(random.random())
                self.local.number=[]
                for i in range(10):
                        self.local.number.append(random.choice(range(10)))
                print threading.currentThread(),self.local.number
threadLocal=ThreadLocal()
threads=[]
for index in range(5):
        thread = threading.Thread(target=threadLocal.run)
        thread.start()
        threads.append(thread)
for i in range(5):
        threads[i].join
print "Main Thread exit...."



Main Thread exit....
[1, 0, 7, 0, 4, 8, 4, 3, 6, 6]
[3, 3, 0, 1, 5, 3, 2, 1, 7, 1]
[2, 2, 2, 1, 0, 2, 2, 3, 9, 5]
[3, 6, 1, 0, 6, 9, 3, 1, 5, 1]
[8, 4, 5, 9, 3, 8, 4, 5, 4, 7]

阅读(1311) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~