Concurrent programming in Java INF4140 04.10.12 Lecture 5 0 Book: Andrews - ch.05 (5.4) Book: Magee & Kramer ch.04 - ch.07 INF4140 (04.10.12) Concurrent programming in Java Lecture 5 1 / 33
Outline 1 Monitors: review 2 Threads in Java: Thread class Runnable interface 3 Example: Interference and Java threads 4 Synchronized blocks and methods: (atomic regions and monitors) 5 Example: No interference and Java threads 6 Example:The ornamental garden 7 Condition synchronization (wait and signal) 8 Example: Mutual exclusion 9 Example: Readers/writers INF4140 (04.10.12) Concurrent programming in Java Lecture 5 2 / 33
Summary of the Last Lecture A monitor encapsulates data, which can only be observed and modified by the monitor s procedures. Contains variables that describe the state Variables can be changed only through the available procedures Implicit mutex: Only a procedure may be active at a time. Two procedures in the same monitor are never executed concurrently Condition synchronization (block a process until a particular condition holds) is given by condition variables. Signaling disciplines: Signal and Wait (SW): the signaller waits, and the signalled process gets to execute immediately Signal and Continue (SC): the signaller continues, and the signalled process executes later INF4140 (04.10.12) Concurrent programming in Java Lecture 5 3 / 33
Java From Wikipedia:... Java is a general-purpose, concurrent, class-based, object-oriented language... INF4140 (04.10.12) Concurrent programming in Java Lecture 5 4 / 33
Threads in Java A thread (processes from previous lectures) in Java has its own stack, its own execution context and it has access to a share state. Threads may be created and deleted dynamically. Thread run() MyThread run() The Thread class executes instructions from its method run(). The actual code executed depends on the implementation provided for run() in a derived class. c l a s s MyThread extends Thread { p u b l i c v o i d run ( ) { //...... // C r e a t i n g a t h r e a d o b j e c t : Thread a = new MyThread ( ) ; a. s t a r t ( ) ; INF4140 (04.10.12) Concurrent programming in Java Lecture 5 5 / 33
Threads in Java Since Java does not permit multiple inheritance, we often implement the run() method in a class not derived from Thread but from the interface Runnable. Runnable target Thread run() MyRun run() public interface Runnable { public abstract void run(); class MyRun implements Runnable { public void run() { //... // C r e a t i n g a t h r e a d o b j e c t : Runnable b = new MyRun ( ) ; new Thread ( b ). s t a r t ( ) ; INF4140 (04.10.12) Concurrent programming in Java Lecture 5 6 / 33
Threads in Java To summarize: Four steps to create a thread in Java: 1 Define a new class that extends the Java Thread class or that implements the Runnable interface. 2 Define a run method in the new class. 3 Create an instance of the new class. 4 Start the thread. INF4140 (04.10.12) Concurrent programming in Java Lecture 5 7 / 33
Example: Interference and Java Threads... c l a s s S t o r e { p r i v a t e i n t data = 0 ; p u b l i c v o i d update ( ) { data++;... // i n a method : S t o r e s = new S t o r e ( ) ; // t h e t h r e a d s below have a c c e s s to s t1 = new FooThread ( s ) ; t1. s t a r t ( ) ; t2 = new FooThread ( s ) ; t2. s t a r t ( ) ; Threads t1 and t2 could execute s.update() concurrently! Interference between t1 and t2 may lose updates to data. INF4140 (04.10.12) Concurrent programming in Java Lecture 5 8 / 33
Synchronized Methods To avoid interference: Threads synchronize access to shared data Mechanism: 1 One unique lock for each object o. 2 mutex: at most one thread T can lock o at any time. 3 T makes a request to lock o in order to execute the block (statements) B as follows: s y n c h r o n i z e d ( o ) { B or T makes a request to lock o in order to execute the whole method m as follows: s y n c h r o n i z e d Type m (... ) {... INF4140 (04.10.12) Concurrent programming in Java Lecture 5 9 / 33
Example part II: No Interference Solution to earlier problem: lock the Store objects before executing problematic method: c l a s s S t o r e { p r i v a t e i n t data = 0 ; or p u b l i c v o i d update ( ) { s y n c h r o n i z e d ( t h i s ) { data++; c l a s s S t o r e { p r i v a t e i n t data = 0 ; p u b l i c s y n c h r o n i z e d v o i d update ( ) { data++;... // i n s i d e a method : S t o r e s = new S t o r e ( ) ; Now only one thread at a time can run s.update(). INF4140 (04.10.12) Concurrent programming in Java Lecture 5 10 / 33
Java Examples Book: Concurrency: State Models & Java Programs, 2 nd Edition Jeff Magee & Jeff Kramer Wiley Examples in Java: http://www.doc.ic.ac.uk/~jnm/book/ INF4140 (04.10.12) Concurrent programming in Java Lecture 5 11 / 33
Ornamental Garden Problem People enter an ornamental garden through either of two turnstiles. Management wants to know how many people are in the garden at any time. The concurrent program consists of: Two concurrent threads Shared counter object INF4140 (04.10.12) Concurrent programming in Java Lecture 5 12 / 33
Ornamental Garden Problem: Class Diagram The Turnstile thread simulates the periodic arrival of a visitor to the garden every second by sleeping for a second and then invoking the increment() method of the counter object. INF4140 (04.10.12) Concurrent programming in Java Lecture 5 13 / 33
Ornamental Garden Problem c l a s s Counter { i n t v a l u e = 0 ; NumberCanvas d i s p l a y ; Counter ( NumberCanvas n ) { d i s p l a y. s e t v a l u e ( v a l u e ) ; v o i d i n c r e m e n t ( ) { i n t temp = v a l u e ; // r e a d [ v ] S i m u l a t e. HWinterrupt ( ) ; v a l u e = temp + 1 ; // w r i t e [ v +1] d i s p l a y. s e t v a l u e ( v a l u e ) ; d i s p l a y = n ; INF4140 (04.10.12) Concurrent programming in Java Lecture 5 14 / 33
Ornamental Garden Problem c l a s s T u r n s t i l e extends Thread { NumberCanvas d i s p l a y ; // i n t e r f a c e Counter p e o p l e ; // s h a r e data T u r n s t i l e ( NumberCanvas n, Counter c ) { // c o n s t r u c t o r d i s p l a y = n ; p e o p l e = c ; p u b l i c v o i d run ( ) { t r y { d i s p l a y. s e t v a l u e ( 0 ) ; f o r ( i n t i = 1 ; i <= Garden.MAX; i ++) { Thread. s l e e p ( 5 0 0 ) ; // 0. 5 second d i s p l a y. s e t v a l u e ( i ) ; p e o p l e. i n c r e m e n t ( ) ; // i n c r e m e n t t h e c o u n t e r catch ( I n t e r r u p t e d E x c e p t i o n e ) { INF4140 (04.10.12) Concurrent programming in Java Lecture 5 15 / 33
Ornamental Garden Program The Counter object and Turnstile threads are created by the go() method of the Garden applet: p r i v a t e v o i d go ( ) { c o u n t e r = new Counter ( counterd ) ; west = new T u r n s t i l e ( westd, c o u n t e r ) ; e a s t = new T u r n s t i l e ( eastd, c o u n t e r ) ; west. s t a r t ( ) ; e a s t. s t a r t ( ) ; INF4140 (04.10.12) Concurrent programming in Java Lecture 5 16 / 33
Ornamental Garden Program: DEMO DEMO After the East and West turnstile threads have each incremented its counter 20 times, the garden people counter is not the sum of the counts displayed. Counter increments have been lost. Why? INF4140 (04.10.12) Concurrent programming in Java Lecture 5 17 / 33
Mutual Exclusion: The Ornamental Garden c l a s s S y n c h r o n i z e d C o u n t e r extends Counter { S y n c h r o n i z e d C o u n t e r ( NumberCanvas n ) { s u p e r ( n ) ; s y n c h r o n i z e d v o i d i n c r e m e n t ( ) { s u p e r. i n c r e m e n t ( ) ; INF4140 (04.10.12) Concurrent programming in Java Lecture 5 18 / 33
Mutual Exclusion: The Ornamental Garden - DEMO DEMO INF4140 (04.10.12) Concurrent programming in Java Lecture 5 19 / 33
Condition Synchronization (Monitors) The following statements are defined for an arbitrary Java object o and must be executed within synchronized portions of code: o.wait(): release lock on o, enter o s wait queue and wait o.notify(): wake up one thread in o s wait queue o.notifyall(): wake up all threads in o s wait queue There is only one (implicit) condition variable per object (wait queue). Objects that wait on o (o.wait()) are in this implicit queue. The ordering of the wait queue is implementation-dependent (usually FIFO). Signaling Discipline: Signal and Continue An awakened thread has no advantage in the competition for the lock to o. INF4140 (04.10.12) Concurrent programming in Java Lecture 5 20 / 33
A Semaphore Implementation in Java // up ( ) i s t h e V o p e r a t i o n // down ( ) i s t h e P o p e r a t i o n p u b l i c c l a s s Semaphore { p r i v a t e i n t v a l u e ; p u b l i c Semaphore ( i n t i n i t i a l ) { v a l u e = i n i t i a l ; s y n c h r o n i z e d p u b l i c v o i d up ( ) { ++v a l u e ; n o t i f y A l l ( ) ; s y n c h r o n i z e d p u b l i c v o i d down ( ) throws I n t e r r u p t e d E x c e p t i o n { w h i l e ( v a l u e ==0) w a i t ( ) ; v a l u e ; INF4140 (04.10.12) Concurrent programming in Java Lecture 5 21 / 33
Mutual Exclusion with Sempahores INF4140 (04.10.12) Concurrent programming in Java Lecture 5 22 / 33
Mutual Exclusion with Sempahores c l a s s MutexLoop implements Runnable { Semaphore mutex ; MutexLoop ( Semaphore sema ) { mutex=sema ; p u b l i c v o i d run ( ) { t r y { w h i l e ( t r u e ) { w h i l e (! ThreadPanel. r o t a t e ( ) ) ; // g e t mutual e x c l u s i o n mutex. down ( ) ; w h i l e ( ThreadPanel. r o t a t e ( ) ) ; // c r i t i c a l s e c t i o n // r e l e a s e mutual e x c l u s i o n mutex. up ( ) ; catch ( I n t e r r u p t e d E x c e p t i o n e ){ DEMO INF4140 (04.10.12) Concurrent programming in Java Lecture 5 23 / 33
Readers and Writers problem A shared database is accessed by two kinds of processes. Readers execute transactions that examine the database while Writers both examine and update the database. A Writer must have exclusive access to the database; any number of Readers may concurrently access it. INF4140 (04.10.12) Concurrent programming in Java Lecture 5 24 / 33
Readers and Writers problem c l a s s Reader implements Runnable { ReadWrite m o n i t o r ; Reader ( ReadWrite m o n i t o r ) { m o n i t o r = m o n i t o r ; p u b l i c v o i d run ( ) { t r y { w h i l e ( t r u e ) { w h i l e (! ThreadPanel. r o t a t e ( ) ) ; // b e g i n c r i t i c a l s e c t i o n m o n i t o r. a c q u i r e R e a d ( ) ; w h i l e ( ThreadPanel. r o t a t e ( ) ) ; m o n i t o r. r e l e a s e R e a d ( ) ; catch ( I n t e r r u p t e d E x c e p t i o n e ){ INF4140 (04.10.12) Concurrent programming in Java Lecture 5 25 / 33
Readers and Writers problem c l a s s W r i t e r implements Runnable { ReadWrite m o n i t o r ; W r i t e r ( ReadWrite m o n i t o r ) { m o n i t o r = m o n i t o r ; p u b l i c v o i d run ( ) { t r y { w h i l e ( t r u e ) { w h i l e (! ThreadPanel. r o t a t e ( ) ) ; // b e g i n c r i t i c a l s e c t i o n m o n i t o r. a c q u i r e W r i t e ( ) ; w h i l e ( ThreadPanel. r o t a t e ( ) ) ; m o n i t o r. r e l e a s e W r i t e ( ) ; catch ( I n t e r r u p t e d E x c e p t i o n e ){ INF4140 (04.10.12) Concurrent programming in Java Lecture 5 26 / 33
Readers and Writers problem i n t e r f a c e ReadWrite { p u b l i c v o i d a c q u i r e R e a d ( ) throws I n t e r r u p t e d E x c e p t i o n ; p u b l i c v o i d r e l e a s e R e a d ( ) ; p u b l i c v o i d a c q u i r e W r i t e ( ) throws I n t e r r u p t e d E x c e p t i o n ; p u b l i c v o i d r e l e a s e W r i t e ( ) ; INF4140 (04.10.12) Concurrent programming in Java Lecture 5 27 / 33
Readers and Writers problem c l a s s ReadWriteSafe implements ReadWrite { p r i v a t e i n t r e a d e r s =0; p r i v a t e boolean w r i t i n g = f a l s e ; p u b l i c s y n c h r o n i z e d v o i d a c q u i r e R e a d ( ) throws I n t e r r u p t e d E x c e p t i o n { w h i l e ( w r i t i n g ) w a i t ( ) ; ++r e a d e r s ; p u b l i c s y n c h r o n i z e d v o i d r e l e a s e R e a d ( ) { r e a d e r s ; i f ( r e a d e r s ==0) n o t i f y A l l ( ) ; p u b l i c s y n c h r o n i z e d v o i d a c q u i r e W r i t e ( ) {... p u b l i c s y n c h r o n i z e d v o i d r e l e a s e W r i t e ( ) {... INF4140 (04.10.12) Concurrent programming in Java Lecture 5 28 / 33
Readers and Writers problem c l a s s ReadWriteSafe implements ReadWrite { p r i v a t e i n t r e a d e r s =0; p r i v a t e boolean w r i t i n g = f a l s e ; p u b l i c s y n c h r o n i z e d v o i d a c q u i r e R e a d ( ) {... p u b l i c s y n c h r o n i z e d v o i d r e l e a s e R e a d ( ) {... p u b l i c s y n c h r o n i z e d v o i d a c q u i r e W r i t e ( ) throws I n t e r r u p t e d E x c e p t i o n { w h i l e ( r e a d e r s >0 w r i t i n g ) w a i t ( ) ; w r i t i n g = t r u e ; p u b l i c s y n c h r o n i z e d v o i d r e l e a s e W r i t e ( ) { w r i t i n g = f a l s e ; n o t i f y A l l ( ) ; DEMO INF4140 (04.10.12) Concurrent programming in Java Lecture 5 29 / 33
Readers and Writers problem - Fairness INF4140 (04.10.12) Concurrent programming in Java Lecture 5 30 / 33
Readers and Writers problem c l a s s R e a d W r i t e F a i r implements ReadWrite { p r i v a t e i n t r e a d e r s =0; p r i v a t e boolean w r i t i n g = f a l s e ; p r i v a t e i n t waitingw = 0 ; // no o f w a i t i n g W r i t e r s. p r i v a t e boolean r e a d e r s t u r n = f a l s e ; s y n c h r o n i z e d p u b l i c v o i d a c q u i r e R e a d ( ) throws I n t e r r u p t e d E x c e p t i o n { w h i l e ( w r i t i n g ( waitingw >0 &&! r e a d e r s t u r n ) ) w a i t ( ) ; ++r e a d e r s ; s y n c h r o n i z e d p u b l i c v o i d r e l e a s e R e a d ( ) { r e a d e r s ; r e a d e r s t u r n=f a l s e ; i f ( r e a d e r s ==0) n o t i f y A l l ( ) ; s y n c h r o n i z e d p u b l i c v o i d a c q u i r e W r i t e ( ) {... s y n c h r o n i z e d p u b l i c v o i d r e l e a s e W r i t e ( ) {... INF4140 (04.10.12) Concurrent programming in Java Lecture 5 31 / 33
Readers and Writers problem c l a s s R e a d W r i t e F a i r implements ReadWrite { p r i v a t e i n t r e a d e r s =0; p r i v a t e boolean w r i t i n g = f a l s e ; p r i v a t e i n t waitingw = 0 ; // no o f w a i t i n g W r i t e r s. p r i v a t e boolean r e a d e r s t u r n = f a l s e ; s y n c h r o n i z e d p u b l i c v o i d a c q u i r e R e a d ( ) {... s y n c h r o n i z e d p u b l i c v o i d r e l e a s e R e a d ( ) {... s y n c h r o n i z e d p u b l i c v o i d a c q u i r e W r i t e ( ) throws I n t e r r u p t e d E x c e p t i o n { ++waitingw ; w h i l e ( r e a d e r s >0 w r i t i n g ) w a i t ( ) ; waitingw ; w r i t i n g = t r u e ; s y n c h r o n i z e d p u b l i c v o i d r e l e a s e W r i t e ( ) { w r i t i n g = f a l s e ; r e a d e r s t u r n=t r u e ; n o t i f y A l l ( ) ; INF4140 (04.10.12) Concurrent programming in Java Lecture 5 32 / 33
Readers and Writers problem DEMO INF4140 (04.10.12) Concurrent programming in Java Lecture 5 33 / 33