-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreading.py
50 lines (37 loc) · 1.34 KB
/
Threading.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
import threading
import time
# Function to simulate a time-consuming task
def task(name, duration):
print(f"Thread {name}: Starting")
time.sleep(duration)
print(f"Thread {name}: Completed after {duration} seconds")
# 1. Creating and starting threads
thread1 = threading.Thread(target=task, args=("Thread-1", 2))
thread2 = threading.Thread(target=task, args=("Thread-2", 3))
thread1.start()
thread2.start()
# 2. Waiting for threads to finish
thread1.join()
thread2.join()
# 3. Daemon threads (threads that run in the background)
daemon_thread = threading.Thread(target=task, args=("Daemon-Thread", 1))
daemon_thread.daemon = True # Mark the thread as a daemon thread
daemon_thread.start()
# Main thread continues to execute while daemon thread runs in the background
# 4. Thread synchronization with locks
counter = 0
lock = threading.Lock()
def increment_counter():
global counter
for _ in range(1000000):
with lock:
counter += 1
# Create and start two threads to increment the counter
thread3 = threading.Thread(target=increment_counter)
thread4 = threading.Thread(target=increment_counter)
thread3.start()
thread4.start()
thread3.join()
thread4.join()
print(f"Final Counter Value: {counter}")
# Note: Ensure you have a clear understanding of the threading concepts and the Global Interpreter Lock (GIL) in Python.