Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 G53SRP: Java Concurrency Control (1) - synchronisation Chris Greenhalgh School of Computer Science.

Similar presentations


Presentation on theme: "1 G53SRP: Java Concurrency Control (1) - synchronisation Chris Greenhalgh School of Computer Science."— Presentation transcript:

1 1 G53SRP: Java Concurrency Control (1) - synchronisation Chris Greenhalgh School of Computer Science

2 2 Contents Overview of Java synchronisationOverview of Java synchronisation Instance methodsInstance methods Class methodsClass methods Synchronized blocksSynchronized blocks Additional NotesAdditional Notes DeadlockDeadlock SummarySummary Book: Wellings 3.1, 1.1 (deadlock)Book: Wellings 3.1, 1.1 (deadlock)

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

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

5 5 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; } … }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)

6 6 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  final balance not 100: Interleaving of shared state reads/write  RW/WW conflicts  error Watch for this

7 7 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; } … }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  Final balance 100 – each method obtains instance lock and so runs to completion without interleaving

8 8 Example 1b Provided balance is only manipulated from synchronized methods all updates will be serialisedProvided balance is only manipulated from synchronized methods all updates will be serialised –i.e. “mutual exclusion”, “critical sections” N.B. each instance is independentN.B. each instance is independent –own balance, own lock What effect does locking have on speed of execution?What effect does locking have on speed of execution? –Considerably slower (time to acquire and release lock) Lock Unsync. Method Object - balance withdraw deposit

9 9 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; } … }public class ClassMethodNoSyncTest { // account balance static int balance; // method public static void deposit() { balance = balance + 100; } // method public static void withdraw() { balance = balance - 100; } … }

10 10 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)  same as in case of shared instance variable (still shared state)

11 11 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; } … }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  Correct execution – methods obtain class lock

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

13 13 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; } … }public class BlockSyncTest { // like NoSyncTest // account balance int balance; // method public void deposit() { balance = balance + 100; } // method public void withdraw() { balance = balance - 100; } … }

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

15 15 Example 3a (cont.) BlockSyncTest instance = new BlockSyncTest(); instance.balance = 100 Thread t1 = new Thread (new UpdateTask(instance)); t1.start(); Thread t2 = new Thread (new UpdateTask(instance)); t2.start(); t1.join(); t2.join();BlockSyncTest instance = new BlockSyncTest(); instance.balance = 100 Thread t1 = new Thread (new UpdateTask(instance)); t1.start(); Thread t2 = new Thread (new UpdateTask(instance)); t2.start(); t1.join(); t2.join();  critical sections execute with lock on shared instance  critical sections execute with lock on shared instance Lock withdraw deposit instance - balance

16 16 Additional notes Object locks are re-entrantObject 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 leftLocks are implicitly released when block or method is left –Avoids problems with explicit lock/unlock of missing an unlock  deadlock

17 17 Additional notes: deadlock A synchronized method can call both synchronized and unsynchronized methods in other classesA synchronized method can call both synchronized and unsynchronized methods in other classes –The lock is still held This can result in deadlock…This can result in deadlock…

18 18 Example of deadlock thread A has lock on O1 and calls sync method on O2 while thread B has lock on O2 and calls sync method on O1thread A has lock on O1 and calls sync method on O2 while thread B has lock on O2 and calls sync method on O1 Lock m1 O1 Lock m2 O2 B A x

19 19 Requirements for deadlock Mutual exclusionMutual exclusion –Only one thread can use each resource (lock) Hold and waitHold and wait –Blocked threads hold onto locks No pre-emptionNo pre-emption –The system will not take away a lock from a blocked thread Circular waitCircular wait –A cycle of threads requiring resources held by the next

20 20 Summary Mutual exclusion is supported on Java by per- object locksMutual exclusion is supported on Java by per- object locks –A class is also an object ( MyClass.class ) Locks are re-entrantLocks are re-entrant Locks are implicitly manipulated by the JVMLocks are implicitly manipulated by the JVM Acquired by synchronized methodsAcquired by synchronized methods –Instance method  instance lock; class method  class lock Or synchronized(…) {…} blocksOr synchronized(…) {…} blocks Released automatically on leaving method/blockReleased automatically on leaving method/block


Download ppt "1 G53SRP: Java Concurrency Control (1) - synchronisation Chris Greenhalgh School of Computer Science."

Similar presentations


Ads by Google