🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The threading.Barrier class in Python's threading module provides a way to synchronize a fixed number of threads. This is useful for scenarios where multiple threads need to wait for each other to reach a certain point before continuing execution.
Table of Contents
- Introduction
threading.BarrierClass Syntax- Examples
- Basic Usage
- Using a Barrier with Multiple Threads
- Barrier with Timeout
- Real-World Use Case
- Conclusion
Introduction
The threading.Barrier class is used to synchronize a fixed number of threads, known as parties. Each thread waits at the barrier until all parties have reached it. Once all parties have reached the barrier, they are released simultaneously.
threading.Barrier Class Syntax
Here is how you create and use a barrier with the threading.Barrier class:
import threading
barrier = threading.Barrier(parties, action=None, timeout=None)
Parameters:
parties: The number of threads that must callwait()before they are all released.action: Optional. A callable that is called by one of the threads when they are released.timeout: Optional. The default timeout value for allwait()calls.
Methods:
wait(timeout=None): Wait for all parties to call this method. When they all have, they are all released simultaneously. An optionaltimeoutcan be provided to override the default.reset(): Reset the barrier to the initial state.abort(): Put the barrier into a broken state.broken: A property that isTrueif the barrier is in a broken state.
Examples
Basic Usage
Create and use a barrier to synchronize threads.
Example
import threading
import time
def worker(barrier, worker_id):
print(f"Worker {worker_id} waiting at barrier")
barrier.wait()
print(f"Worker {worker_id} passed barrier")
barrier = threading.Barrier(3)
threads = [threading.Thread(target=worker, args=(barrier, i)) for i in range(3)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
Output:
Worker 0 waiting at barrier
Worker 1 waiting at barrier
Worker 2 waiting at barrier
Worker 0 passed barrier
Worker 1 passed barrier
Worker 2 passed barrier
Using a Barrier with Multiple Threads
Synchronize more threads using a barrier.
Example
import threading
import time
def worker(barrier, worker_id):
time.sleep(worker_id)
print(f"Worker {worker_id} waiting at barrier")
barrier.wait()
print(f"Worker {worker_id} passed barrier")
barrier = threading.Barrier(5)
threads = [threading.Thread(target=worker, args=(barrier, i)) for i in range(5)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
Output:
Worker 0 waiting at barrier
Worker 1 waiting at barrier
Worker 2 waiting at barrier
Worker 3 waiting at barrier
Worker 4 waiting at barrier
Worker 0 passed barrier
Worker 1 passed barrier
Worker 2 passed barrier
Worker 3 passed barrier
Worker 4 passed barrier
Barrier with Timeout
Use a barrier with a timeout to handle situations where threads might not reach the barrier in time.
Example
import threading
import time
def worker(barrier, worker_id, delay):
time.sleep(delay)
print(f"Worker {worker_id} waiting at barrier")
try:
barrier.wait(timeout=3)
print(f"Worker {worker_id} passed barrier")
except threading.BrokenBarrierError:
print(f"Worker {worker_id} barrier broken")
barrier = threading.Barrier(3)
threads = [threading.Thread(target=worker, args=(barrier, i, i * 2)) for i in range(3)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
Output:
Worker 0 waiting at barrier
Worker 1 waiting at barrier
Worker 2 waiting at barrier
Worker 0 passed barrier
Worker 1 barrier broken
Worker 2 barrier broken
Real-World Use Case
Synchronizing Phases in a Multi-Step Process
Use a barrier to synchronize threads at different phases of a multi-step process.
Example
import threading
import time
def phase1(barrier):
print("Phase 1 started")
time.sleep(2)
print("Phase 1 waiting at barrier")
barrier.wait()
print("Phase 1 completed")
def phase2(barrier):
print("Phase 2 started")
time.sleep(1)
print("Phase 2 waiting at barrier")
barrier.wait()
print("Phase 2 completed")
barrier = threading.Barrier(2)
thread1 = threading.Thread(target=phase1, args=(barrier,))
thread2 = threading.Thread(target=phase2, args=(barrier,))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
Output:
Phase 1 started
Phase 2 started
Phase 2 waiting at barrier
Phase 1 waiting at barrier
Phase 2 completed
Phase 1 completed
Conclusion
The threading.Barrier class is used for synchronizing threads in multithreaded programs. It allows threads to wait for each other at a specific point, ensuring that they proceed together. This is particularly useful in scenarios where threads need to coordinate their actions at certain stages of execution. Proper usage can significantly enhance the reliability and control of your concurrent applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment