Lec5 - Thread Communication Shared Objects Shared Object...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Shared Objects Abhik Roychoudhury CS 3211 National University of Singapore CS3211 2009-10 by Abhik Modified from Kramer and Magee’s lecture notes. Reading material: Chapter 4 of Textbook. 1 Thread Communication Shared Object CS3211 2009-10 by Abhik 2 (Passive) Thread Object (Active) Thread Object (Active) Arbitrary interleaving of accesses possible. Interference between threads class Counter { private int c = 0; public void increment() { c++; } public void decrement() { c--; } public int value() { CS3211 2009-10 by Abhik 3 return c; } } “Correct” operation: One execution of increment adds 1 One execution of decrement subtracts 1 Inter-thread interference from prevent the result from being so. Why? Interference between Threads – (1) Shared Memory Init: c == 0 Thread A Thread B Read c == 0 Read c == 0 Incr., Get 1 CS3211 2009-10 by Abhik 4 Decr., get -1 Write c = -1 Write c = 1 Executing the Statement c++; Executing the Statement c--; Interference between Threads – (2) Shared Memory Init: c == 0 Thread A Thread B Read c == 0 Read c == 0 Incr., Get 1 CS3211 2009-10 by Abhik 5 Decr., get -1 Write c = -1 Write c = 1 Executing the Statement c++; Executing the Statement c--; What do we need? ` Mutually exclusive access to the counter ` How to do that? ` Language level construct – Lock. ` Acquire lock prior to any access of counter. ` Release lock after any access of counter. ` Does it require locking discipline then? ` Well, accesses happen through methods of the shared object ` In this case, objects of the Counter class ` Mark these methods as “synchronized” ` Avoid managing locks for each call of these methods !! CS3211 2009-10 by Abhik 6
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Synchronized Methods Java programming provides two basic synchronization idioms: synchronized methods and synchronized statements public class SynchronizedCounter { private int c = 0; public synchronized void increment() { c++; } public synchronized void decrement() { CS3211 2009-10 by Abhik 7 c--; } public synchronized int value() { return c; } } Concepts : process interference. mutual exclusion . Models : model checking for interference modeling mutual exclusion Practice : thread interference in shared Java objects mutual exclusion in Java ( synchronized objects/methods). Why synchronized methods? 1. It is not possible for two invocations of synchronized methods on the same object to overlap. 2. When a synchronized method exits, it makes the object state visible to all threads accessing the object subsequently via synchronized methods. Lock y = 1 x = 1 Thread 1 CS3211 2009-10 by Abhik 9 Unlock Lock i= x j = y unlock 1 1 Thread 2 4.1 Interference Garden People enter an ornamental garden through either of two turnstiles. Management wish to know how many are in the garden at any time. Ornamental garden problem:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/02/2011 for the course CS 3211 taught by Professor Dunnowho during the Spring '11 term at National University of Singapore.

Page1 / 6

Lec5 - Thread Communication Shared Objects Shared Object...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online