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
increment
method is declared assynchronized
to ensure that only one thread can execute this method at a time, making it thread-safe.
- Contains the shared resource
CounterIncrementer Class:
- Extends the
Thread
class and is responsible for incrementing the counter. - In the
run
method, it loops to increment the counter 1000 times. - The
CounterIncrementer
constructor takes aCounter
object as a parameter to ensure the shared resource is accessed.
- Extends the
Main Class:
- Creates a shared
Counter
object. - Creates two
CounterIncrementer
threads, each initialized with the sharedCounter
instance. - 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
increment
method uses a synchronized block to ensure thread-safe access to thecount
variable. Thelock
object is used as a monitor to synchronize the block.
- Contains the shared resource
CounterIncrementer Class:
- Extends the
Thread
class and is responsible for incrementing the counter. - In the
run
method, it loops to increment the counter 1000 times. - The
CounterIncrementer
constructor takes aCounter
object as a parameter to ensure the shared resource is accessed.
- Extends the
Main Class:
- Creates a shared
Counter
object. - Creates two
CounterIncrementer
threads, each initialized with the sharedCounter
instance. - 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
ReentrantLock
object to ensure thread-safe access to thecount
variable. - The
increment
method locks the lock before updatingcount
and unlocks it in afinally
block to ensure that the lock is released even if an exception is thrown.
- Contains the shared resource
CounterIncrementer Class:
- Extends the
Thread
class and is responsible for incrementing the counter. - In the
run
method, it loops to increment the counter 1000 times. - The
CounterIncrementer
constructor takes aCounter
object as a parameter to ensure the shared resource is accessed.
- Extends the
Main Class:
- Creates a shared
Counter
object. - Creates two
CounterIncrementer
threads, each initialized with the sharedCounter
instance. - 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