L21cs2110fa09-6up - 08/11/2009 Reminder 2 A race condition...

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

View Full Document Right Arrow Icon
08/11/2009 1 Lecture 21 – CS2110 – Fall 2009 RACE CONDITIONS AND SYNCHRONIZATION Reminder ± A “race condition” arises if two threads try and share some data ± One updates it and the other reads it, or both update the data 2 ± In such cases it is possible that we could see the data “in the middle” of being updated ± A “race condition”: correctness depends on the update racing to completion without the reader managing to glimpse the in-progress update ± Synchronization (aka mutual exclusion) solves this Java Synchronization (Locking) 3 private Stack<String> stack = new Stack<String>(); public void doSomething() { synchronized ( stack ) { if ( stack .isEmpty()) return ; String s = stack .pop(); } //d thi ith //do something with s. .. } • Put critical operations in a synchronized block • The stack object acts as a lock • Only one thread can own the lock at a time synchronized block Java Synchronization (Locking) 4 public synchronized void doSomething() { ... } •You can lock on any object, including this public void doSomething() { synchronized ( this ) { ... } } is equivalent to How locking works ± Only one thread can “hold” a lock at a time ± If several request the same lock, Java somehow decides which will get it ± The lock is released when the thread leaves 5 the synchronization block ± synchronized(someObject) { protected code } ± The protected code has a mutual exclusion guarantee: At most one thread can be in it ± When released, some other thread can acquire the lock Locks are associated with objects ± Every Object has its own built-in lock ± Just the same, some applications prefer to create special classes of objects to use just for locking ± This is a stylistic decision and you should agree 6 on it with your teammates or learn the company policy if you work at a company ± Code is “thread safe” if it can handle multiple threads using it… otherwise it is “unsafe”
Background image of page 1

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

View Full DocumentRight Arrow Icon
08/11/2009 2 File Locking: Same idea 7 • In file systems, if two or more processes could modify a file simultaneously, this could result in data corruption • A process must open a file to modify it – A process must a file to modify it gives exclusive access until it is closed • Multiple processes can open the same file to read it •Th is file locking synchronization rule is enforced by the operating system Deadlock 8 •The downside of locking – deadlock •A deadlock occurs when two or more competing threads each hold a lock, and each are waiting for the other to relinquish a each are waiting for the other to relinquish a lock, so neither ever does •Example:
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.

Page1 / 5

L21cs2110fa09-6up - 08/11/2009 Reminder 2 A race condition...

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