您的位置:首页 > 编程语言 > Python开发

Python多线程简单例子

2017-05-01 18:17 423 查看
直接附上代码:

#!/usr/bin/python
# coding:utf-8
import threading
import time

class Test(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
# self._run_num = num

def run(self):
global count, mutex
threadname = threading.currentThread().getName()

# for x in xrange(0, int(self._run_num)):
mutex.acquire()
count += 1
mutex.release()
print '线程信息名字:', threadname
print '在这个线程中的编号: ', x
print '累计和: ', count
# time.sleep(1)

global count, mutex
threads = []
num = 4
count = 0
mutex = threading.Lock()

for x in xrange(0, num):
threads.append(Test())

for t in threads:
t.start()

for t in threads:
t.join()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 多线程