-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreading.py
More file actions
30 lines (24 loc) · 794 Bytes
/
threading.py
File metadata and controls
30 lines (24 loc) · 794 Bytes
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
import threading
import time
# function to print numbers with a delay
def print_numbers(thread_name, start, end, delay):
for i in range(start, end):
print(f"{thread_name}: {i}")
time.sleep(delay)
# function to create and start threads
def main():
# list of threads
threads = [
threading.Thread(target=print_numbers, args=("Thread-1", 1, 5, 1)),
threading.Thread(target=print_numbers, args=("Thread-2", 5, 10, 0.5)),
threading.Thread(target=print_numbers, args=("Thread-3", 10, 15, 0.2))
]
# starting all threads
for thread in threads:
thread.start()
# wait for all threads to complete
for thread in threads:
thread.join()
print("All threads have finished.")
if __name__ == "__main__":
main()