Thread Synchronization in Java
Thread Synchronization in Java
- Synchronized Method
- Synchronized Block
- Reentrant Locks (in java.util.concurrent.locks package)
Counter Class:
- Contains the shared resource
count. - The
incrementmethod is declared assynchronizedto ensure that only one thread can execute this method at a time, making it thread-safe.
- Contains the shared resource
CounterIncrementer Class:
- Extends the
Threadclass and is responsible for incrementing the counter. - In the
runmethod, it loops to increment the counter 1000 times. - The
CounterIncrementerconstructor takes aCounterobject as a parameter to ensure the shared resource is accessed.
- Extends the
Main Class:
- Creates a shared
Counterobject. - Creates two
CounterIncrementerthreads, each initialized with the sharedCounterinstance. - Starts both threads and waits for them to complete using
join. - Prints the final count.
- Creates a shared
This implementation uses the Thread class directly, demonstrating thread creation and execution by extending Thread. The synchronized method ensures that the increment method is executed atomically by one thread at a time.
A synchronized block is used to synchronize a specific block of code inside a method. It is more efficient than synchronized methods because you can control the scope of synchronization, limiting it to a critical section.
Counter Class:
- Contains the shared resource
count. - The
incrementmethod uses a synchronized block to ensure thread-safe access to thecountvariable. Thelockobject is used as a monitor to synchronize the block.
- Contains the shared resource
CounterIncrementer Class:
- Extends the
Threadclass and is responsible for incrementing the counter. - In the
runmethod, it loops to increment the counter 1000 times. - The
CounterIncrementerconstructor takes aCounterobject as a parameter to ensure the shared resource is accessed.
- Extends the
Main Class:
- Creates a shared
Counterobject. - Creates two
CounterIncrementerthreads, each initialized with the sharedCounterinstance. - Starts both threads and waits for them to complete using
join. - Prints the final count.
- Creates a shared
This implementation uses a synchronized block within the Counter class to control access to the count variable, ensuring that only one thread can increment the counter at a time. The lock object is used to synchronize the block of code that modifies the shared resource.
Reentrant locks provide more flexibility and features compared to synchronized methods/blocks. They are part of the java.util.concurrent.locks package and offer advanced operations like try-lock and timed-lock.
Below is the example implemented using ReentrantLock from the java.util.concurrent.locks package instead of synchronized blocks or methods.
Counter Class:
- Contains the shared resource
count. - Uses a
ReentrantLockobject to ensure thread-safe access to thecountvariable. - The
incrementmethod locks the lock before updatingcountand unlocks it in afinallyblock to ensure that the lock is released even if an exception is thrown.
- Contains the shared resource
CounterIncrementer Class:
- Extends the
Threadclass and is responsible for incrementing the counter. - In the
runmethod, it loops to increment the counter 1000 times. - The
CounterIncrementerconstructor takes aCounterobject as a parameter to ensure the shared resource is accessed.
- Extends the
Main Class:
- Creates a shared
Counterobject. - Creates two
CounterIncrementerthreads, each initialized with the sharedCounterinstance. - Starts both threads and waits for them to complete using
join. - Prints the final count.
- Creates a shared
This implementation uses ReentrantLock to control access to the shared count variable. The lock provides more flexibility and control compared to synchronized methods or blocks, allowing for more complex synchronization scenarios. The lock and unlock methods are used to acquire and release the lock, respectively. The use of a finally block ensures that the lock is always released, even if an exception occurs.
Comments
Post a Comment