29. Threading Module
This Python code snippets demonstrating the use of the threading module for implementing multi-threading in Python:
1. Basic Multi-threading
import threading
def print_numbers():
for i in range(5):
print(f"Thread: {threading.current_thread().name} -> {i}")
# Creating threads
thread1 = threading.Thread(target=print_numbers, name="Thread1")
thread2 = threading.Thread(target=print_numbers, name="Thread2")
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print("Execution completed.")2. Using Thread Class with Arguments
3. Using a Custom Thread Class
4. Lock for Thread Synchronization
5. Using Condition for Thread Communication
6. Daemon Threads
7. Thread-safe Queue
8. Using Event for Thread Signaling
9. Using Semaphore to Limit Threads
10. Using Barrier for Thread Coordination
These examples cover basic threading, synchronization tools (e.g., locks, semaphores, and barriers), communication tools (e.g., events and conditions), and custom thread implementations.
Last updated