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

全部博文(103)

文章存档

2014年(8)

2013年(95)

我的朋友

分类: Python/Ruby

2013-12-19 11:40:53

import threading
import time


class Counter:
        def __init__(self):
                self.value=0
                self.lock = threading.Lock()
        def increment(self):
                self.lock.acquire()
                self.value=self.value+1
                value=self.value
                self.lock.release()
                return value


counter = Counter()


class ThreadDemo(threading.Thread):
        def __init__(self,index,create_time):
                threading.Thread.__init__(self)
                self.index = index
                self.create_time = create_time
        def run(self):
                time.sleep(1)
                value = counter.increment()
                print(time.time()-self.create_time),"\t",self.index,"\tvalue:",value
for index in range(100):
        thread = ThreadDemo(index,time.time())
        thread.start()

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