Java Concurrency Control - Synchronization
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 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();
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