-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththreads.py
More file actions
54 lines (42 loc) · 1.28 KB
/
threads.py
File metadata and controls
54 lines (42 loc) · 1.28 KB
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
import threading
import time
class MyThread(threading.Thread):
def __init__(self, id, name, delay, counter, shared_list):
threading.Thread.__init__(self)
self.name = name
self.delay = delay
self.id = id
self.counter = counter
self.result = 0
self.shared_list = shared_list
def run(self):
print('start thread: ' + self.name)
while self.counter > 0:
time.sleep(self.delay)
print('{}: {}'.format(self.name, time.ctime(time.time())))
self.counter -= 1
self.result += self.id
self.shared_list.append(self.name + '-' + str(self.counter))
print('finished thread: ' + self.name)
def get_result(self):
return self.result
shared_list = []
thread1 = MyThread(1, 'Th1', 1, 5, shared_list)
thread2 = MyThread(2, 'Th2', 1.5, 5, shared_list)
thread3 = MyThread(3, 'Th3', 1.5, 5, shared_list)
# Sequentially
#thread1.run()
#thread2.run()
# Run in parallel
#thread1.start()
#thread2.start()
all_thread = [thread1, thread2, thread3]
for th in all_thread:
th.start()
print('Threads started')
for th in all_thread:
th.join()
for th in all_thread:
print('{}: {}'.format(th.name, th.get_result()))
print(shared_list)
print('My program is finished')