■ Thread 클래스를 사용해 스레드 상속을 구현하는 방법을 보여준다.
▶ 예제 코드 (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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import threading import time class TestThread(threading.Thread): def __init__(self, message): threading.Thread.__init__(self) self.message = message self.daemon = True self.event = threading.Event() def run(self): while True: if self.event.is_set(): break; time.sleep(1) print(self.message) threadList = [] for message in ["you", "need", "python"]: thread = TestThread(message) threadList.append(thread) thread.start() for i in range(10): time.sleep(1) print(i) for thread in threadList: thread.event.set() time.sleep(1) """ python need you 0 1 python you need 2 need python you you 3 python need python you need 4 need you 5 python you need 6 python need 7 you python 8 need you python 9 need you python """ |