Python threadingモジュールの利用方法2

サンプルコード:

#-*- encoding: utf-8 -*-
import threading
import time

class Test(threading.Thread):
def __init__(self, num):
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 = count + 1
mutex.release()
print threadname, x, count
time.sleep(1)

if __name__ == '__main__’:
global count, mutex
threads = []
num = 4
count = 1
# ロックの作成
mutex = threading.Lock()
# スレッドオブジェクトを作成
for x in xrange(0, num):
threads.append(Test(10))
# スレッドを開始
for t in threads:
t.start()
# 子スレッドの終了を待つ
for t in threads:
t.join()

Development

Posted by arkgame