直接附上代码:
#!/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()