-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleThread.py
More file actions
156 lines (114 loc) · 2.83 KB
/
SingleThread.py
File metadata and controls
156 lines (114 loc) · 2.83 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import time
import threading
def natural_nums(lst):
for i in lst:
print(f"Number: {i}")
time.sleep(2)
def even_nums(lst):
for i in lst:
print(f"Even Number: {i}")
time.sleep(2)
list1 = [1, 2, 3, 9, 5]
list2 = [2, 4, 6, 8, 10]
t1 = threading.Thread(target=natural_nums, args=(list1,))
t2 = threading.Thread(target=even_nums, args=(list2,))
t1.start()
t2.start()
import threading
import time
def coding():
for i in range(1, 6):
print(f"Coding line {i}")
time.sleep(1)
def error_check():
for i in range(1, 6):
print(f"Checking error in line {i}")
time.sleep(1)
def auto_save():
for i in range(1, 6):
print(f"Auto-saving... ({i})")
time.sleep(1)
def main():
print("Opening VS Code...\n")
time.sleep(1)
# Create threads
t1 = threading.Thread(target=coding)
t2 = threading.Thread(target=error_check)
t3 = threading.Thread(target=auto_save)
# Start threads
t1.start()
t2.start()
t3.start()
# Wait for threads to finish
t1.join()
t2.join()
t3.join()
print("\nClosing VS Code...")
if __name__ == "__main__":
main()
# Easy code version of above code
import threading, time
def task(name, times):
for i in range(1, times+1):
print(f"{name} {i}")
time.sleep(1)
print("Open VS Code")
t1 = threading.Thread(target=task, args=("Coding", 5))
t2 = threading.Thread(target=task, args=("Error Check", 5))
t3 = threading.Thread(target=task, args=("Auto Save", 5))
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
print("Close VS Code")
# gamana code version of above code
import threading
import time
print("VS Code is opened")
def coding():
for i in range(5):
print("coding...")
time.sleep(1)
def errorcheck():
for i in range(5):
print("checking for errors...")
time.sleep(1)
def autosave():
for i in range(5):
print("saving...")
time.sleep(1)
coding()
errorcheck()
autosave()
print("VS Code is closed")
# gamana code version of above code with Multi- threading
import time
import threading
print("VS Code is opened")
def coding():
for i in range(5):
print("coding...")
time.sleep(1)
def errorcheck():
for i in range(5):
print("checking for errors...")
time.sleep(1)
def autosave():
for i in range(5):
print("saving...")
time.sleep(1)
# Create threads
t1 = threading.Thread(target=coding)
t2 = threading.Thread(target=errorcheck)
t3 = threading.Thread(target=autosave)
# Start threads
t1.start()
t2.start()
t3.start()
# Wait for threads to finish
t1.join()
t2.join()
t3.join()
print("VS Code is closed")