0% found this document useful (0 votes)
11 views

Java Concurrency Control - Synchronization

Java provides synchronization through object locks to enable thread-safe access to shared resources. Synchronized methods acquire the relevant lock to ensure only one thread can access synchronized code blocks at a time. This includes instance method locks on the object and class method locks on the class. Synchronized blocks also allow explicitly acquiring an object lock within unsynchronized methods. Locks prevent race conditions and ensure consistent shared state by serializing access.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Concurrency Control - Synchronization

Java provides synchronization through object locks to enable thread-safe access to shared resources. Synchronized methods acquire the relevant lock to ensure only one thread can access synchronized code blocks at a time. This includes instance method locks on the object and class method locks on the class. Synchronized blocks also allow explicitly acquiring an object lock within unsynchronized methods. Locks prevent race conditions and ensure consistent shared state by serializing access.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Java Multi threading

Java Concurrency Control


synchronization

1
Contents
• Overview of Java synchronisation
• Instance methods
• Class methods
• Synchronized blocks
• Additional Notes
• Summary

2
Synchronisation Overview
• Every Java object has an
associated lock
– Every class is also an object and Lock
has its own lock
• At most one thread can hold a Object
lock at any time
• The lock is manipulated Method
implicitly using the
synchronized keyword…

3
Synchronized methods
• If a method is declared Object
synchronized Lock
– The calling thread obtains the lock Sync.
on entry and releases the lock on Method
exit (return or exception) Sync.
– => at most one thread can be Method
active in a sync. method per object
– = “monitor”
• Other methods are always open Unsync.
Method

4
Example 1a: instance method
• public class MethodNoSyncTest {
// account balance
int balance;
// method
public void deposit()
{
balance = balance + 100;
}
// method
public void withdraw()
{
balance = balance - 100;
}

} Critical sections because:
Modify shared state (shared memory)
5
Watch for this

Example 1a (cont.)
class UpdateTask implements Runnable {
public void run() {
for (long i = 0; i < 1000000; i++) {
deposit();
withdraw();
}
}
}

balance = 100;
Thread t1 = new Thread(new UpdateTask());
t1.start();
Thread t2 = new Thread(new UpdateTask());
t2.start();
t1.join();
t2.join();

• → final balance not 100: Interleaving of shared state reads/write →


RW/WW conflicts → error 6
Example 1b
• public class MethodSyncTest {
// account balance
int balance;
// method
public synchronized void deposit()
{
balance = balance + 100;
}
// method
public synchronized void withdraw()
{
balance = balance - 100;
}

}

• → Final balance 100 – each method obtains instance lock and so runs
to completion without interleaving 7
Example 1b
• Provided balance is only Object
manipulated from synchronized
methods all updates will be - balance
serialised Lock
– i.e. “mutual exclusion”,
“critical sections” withdraw
• Each instance is independent
– own balance, own lock deposit
• What effect does locking have
on speed of execution? Unsync.
– Considerably slower Method
(time to acquire and release lock)
8
Example 2a: class method
• public class ClassMethodNoSyncTest {
// account balance
static int balance;
// method
public static void deposit()
{
balance = balance + 100;
}
// method
public static void withdraw()
{
balance = balance - 100;
}

}

9
Example 2a (cont.)
static class UpdateTask implements Runnable {
public void run() {
for (long i = 0; i < 1000000; i++) {
deposit();
withdraw();
}
}
}

balance = 100;
Thread t1 = new Thread(new UpdateTask());
t1.start();
Thread t2 = new Thread(new UpdateTask());
t2.start();
t1.join();
t2.join();

• → same as in case of shared instance variable (still shared state)


10
Example 2b
• public class ClassMethodSyncTest {
// account balance
static int balance;
// method
public static synchronized void deposit()
{
balance = balance + 100;
}
// method
public static synchronized void withdraw()
{
balance = balance - 100;
}

}

• → Correct execution – methods obtain class lock


11
Example 2b
• Class has its own lock Class object
– N.B. independent of all instance - balance (static)
locks
Class lock
– => class synchronized methods do
NOT block instance synchronized static
methods or vice versa withdraw
deposit

Unsync.
Method

12
Example 3a: synchronized block
• public class BlockSyncTest { // like NoSyncTest
// account balance
int balance;
// method
public void deposit()
{
balance = balance + 100;
}
// method
public void withdraw()
{
balance = balance - 100;
}

}

13
Example 3a (cont.)
• class UpdateTask implements Runnable {
UpdateTask(BlockSyncTest instance) {
this.instance = instance;
}
BlockSyncTest instance;
public void run() {
for (long i = 0; i < 1000000; i++) {
synchronized(instance) {
instance.deposit();
instance.withdraw();
}
}
}
}

14
Example 3a (cont.)
• BlockSyncTest instance = new BlockSyncTest();
instance.balance = 100
Thread t1 = new Thread instance
(new UpdateTask(instance)); - balance
t1.start();
Thread t2 = new Thread Lock
(new UpdateTask(instance));
t2.start(); withdraw
t1.join();
t2.join();
deposit
• → critical sections
execute with lock on shared instance
15
Additional notes
• Object locks are re-entrant
– i.e. a sychronized method or block can call
other synchronized methods on the same object
(lock) without blocking/deadlock
• Locks are implicitly released when block or
method is left

16
Summary
• Mutual exclusion is supported on Java by per-
object locks
– A class is also an object (MyClass.class)
• Locks are re-entrant
• Locks are implicitly manipulated by the JVM
• Acquired by synchronized methods
– Instance method → instance lock;
class method → class lock
• Or synchronized(…) {…} blocks
• Released automatically on leaving method/block
17

You might also like