■ Thread 클래스의 start/join 메소드를 사용하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
from threading import Lock from threading import Thread class Counter: def __init__(self): self.lock = Lock() self.count = 0 def increment(self, value): with self.lock: self.count += value def worker(loopCount, counter): for _ in range(loopCount): counter.increment(1) def runThreads(sourceFunction, loopCount, counter): threadList = [] for i in range(5): argumentTuple = (loopCount, counter) thread = Thread(target = sourceFunction, args = argumentTuple) threadList.append(thread) thread.start() for thread in threadList: thread.join() loopCount = 10 ** 5 counter = Counter() runThreads(worker, loopCount, counter) print("loopCount :", loopCount ) print("counter.count :", counter.count) """ loopCount : 100000 counter.count : 500000 """ |