-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_7_8.py
35 lines (30 loc) · 892 Bytes
/
class_7_8.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
import threading, time
class SumThread(threading.Thread):
def __init__(self, count):
threading.Thread.__init__(self)
self.count = count
self.return_value = None # RETURN VALUE
def run(self):
sum_value = 0
for i in range(1, 1 + self.count):
sum_value += i
time.sleep(0.1)
self.return_value = sum_value # SET RETURN VALUE
def get_value(self): # GETTER METHOD
return self.return_value
class OddThread(SumThread):
def run(self):
sum_value = 0
for i in range(1, 1 + self.count):
if i % 2 == 1:
sum_value += i
time.sleep(0.1)
self.return_value = sum_value
thread1 = SumThread(5)
thread2 = OddThread(5)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(thread1.get_value()) # 15
print(thread2.get_value())