![]() |
VOOZH | about |
Synchronization is used to control the execution of multiple processes or threads so that shared resources are accessed in a proper and orderly manner. It helps avoid conflicts and ensures correct results when many tasks run at the same time.
There are three main ways to achieve synchronization.
Synchronized methods are used to lock an entire method so that only one thread can execute it at a time for a particular object. This ensures safe access to shared data but may reduce performance due to full method locking.
Counter: 2000
Explanation: Both threads increment the same counter concurrently. Since the inc() and get() methods are synchronized, only one thread can access them at a time, ensuring the correct final count.
Synchronized blocks allow locking only a specific section of code instead of the entire method. This makes the program more efficient by reducing the scope of synchronization.
Counter: 2000
Explanation: The synchronized block ensures mutual exclusion only for the increment statement, reducing the locking overhead.
Static synchronization is used when static data or methods need to be protected in a multithreaded environment. It ensures that only one thread can access the class-level resource at a time.
1 2 3 10 20 30
Explanation: Both threads t1 and t2 call the static synchronized method printTable(). The lock is applied to the Table.class object, ensuring that only one thread can access the method at a time, even if no object instance is shared.
There are two type of synchronizations in Java which are listed below:
Process synchronization is a fundamental concept in operating systems that ensures multiple processes or threads can execute safely while sharing common resources. Process Synchronization is a technique used to coordinate the execution of multiple processes. It ensures that the shared resources are safe and in order.
Deposited: 200, Balance: 1200 Withdrawn: 100, Balance: 1100 Deposited: 200, Balance: 1300 Deposited: 200, Balance: 1500 Withdrawn: 100, Balance: 1400 Withdrawn: 100, Balance: 1300 Final Balance: 1300
Explanation: Two threads perform deposit and withdrawal operations simultaneously. The synchronized methods prevent race conditions, ensuring consistent balance updates.
Thread Synchronization is used to coordinate and ordering of the execution of the threads in a multi-threaded program. There are two types of thread synchronization are mentioned below:
Example: Ticket Booking System
Booked 2 tickets, Remaining tickets: 8 Booked 3 tickets, Remaining tickets: 5 Booked 3 tickets, Remaining tickets: 2 Booked 2 tickets, Remaining tickets: 0 Final Available Tickets: 0
Explanation: The synchronized bookTicket() method ensures that only one thread books tickets at a time, preventing overbooking and ensuring correct availability.
The volatile keyword in Java ensures that all threads have a consistent view of a variable's value. It prevents caching of the variable's value by threads, ensuring that updates to the variable are immediately visible to other threads.
Working of Volatile Modifier:
Running... Running... Running... Stopped.
Explanation: The volatile variable running ensures that updates made by one thread (in stop()) are visible to the thread running the loop in start().
| volatile | synchronized |
|---|---|
| Ensures visibility of changes across threads | Ensures visibility along with mutual exclusion |
| Does not use any locking | Uses intrinsic locking mechanism |
| Not thread-safe for complex operations | Fully thread-safe |
| Does not guarantee atomicity | Guarantees atomic operations |
| Lightweight and faster | Heavyweight and slower due to locking |
| Used for simple flags or variables | Used for critical sections like counters or transactions |