MPRI – Course on Concurrency Probabilistic methods in Concurrency
Concurrency Patterns
-
Upload
melbournepatterns -
Category
Technology
-
view
5.864 -
download
2
description
Transcript of Concurrency Patterns
![Page 1: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/1.jpg)
Concurrency Patterns
David KempMelbourne Patterns Group
March 2010
Some rights reserved: Creative Commons Attribution-Noncommercial-Share Alikehttp://creativecommons.org/licenses/by-nc-sa/3.0/
![Page 2: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/2.jpg)
Concurrency: what and why?
![Page 3: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/3.jpg)
ThreadThread
Stack
Code
HeapStack
![Page 4: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/4.jpg)
Issues
![Page 5: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/5.jpg)
Race Conditions
public class BankAccount {
private int balance; public void adjustBalance(int amount) { balance = balance + amount; }...}
![Page 6: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/6.jpg)
Memory Consistency (A)
Initially
Thread B
Instructions may be re-ordered
a = true;b = true;
return !b | a;
boolean a = false;boolean b = false;
Thread A
Possible for Thread B to see b as true before a is true.
![Page 7: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/7.jpg)
Memory Consistency (B)
Initially
Thread B
Partially constructed objects can be visible
object = new MyClass(); if (object != null) object.doSomething();
MyClass object = null;
Thread A
object may not be in a consistent state in thread B
![Page 8: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/8.jpg)
If Possible:Avoid Concurrency!!
![Page 9: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/9.jpg)
Concurrency Primitives
![Page 10: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/10.jpg)
Threads (in Java)
Runnable runnable = new MyRunnable(); Thread thread = new Thread(runnable);thread.start();
interface Runnable { void run();}
Looks strangely like the command pattern(covered last month).
The Java ExecutorService interface is even more like the Command Pattern.
![Page 11: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/11.jpg)
Mutex (Mutual Exclusion Lock)
![Page 12: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/12.jpg)
Mutexes can fix race conditions
public class BankAccount { Object lock = new Object(); int balance;
public void adjustBalance(int amount) { synchronize(lock) { balance = balance + amount; } }
![Page 13: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/13.jpg)
Mutexes can fix Memory ConsistencyInitially
Thread B
synchronize(lock) { a = true; b = true;}
synchronize(lock) { return !b | a;}
Object lock = new Object();boolean a = false;boolean b = false;
Thread A Must use the same lock!
Can also fix some memory consistencyproblems using the volatile keyword.
![Page 14: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/14.jpg)
If you need clever analysis to show thread safety, then it is either broken now, or will be broken by some future
maintainer!!!!
![Page 15: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/15.jpg)
Other Concurrency Abstractions• Condition Variables• Semaphores.• Latches.• Barriers.• Futures.• Read/Write Locks.• Blocking Queues.• Exchangers.• Threadpools.• Atomic Compare-And-Set
![Page 16: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/16.jpg)
Some Patterns
![Page 17: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/17.jpg)
Fully Synchronized Objects
Photo by Kevin Hamm. Some rights reserved.See: http://www.flickr.com/photos/krhamm/171302278/in/set-72157594171880205/
![Page 18: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/18.jpg)
Fully Synchronized Object
All methods on a fully synchronized objectare synchronized on a single lock.
![Page 19: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/19.jpg)
Resource Ordering
Image by Benjamin D. Esham for the Wikimedia Commons.Some rights reserved.See: http://en.wikipedia.org/wiki/File:Dining_philosophers.png/
![Page 20: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/20.jpg)
Copy-on-write
![Page 21: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/21.jpg)
Passing immutable objects as messages between threads.
Concurrent modification ceases to be a problem!
![Page 22: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/22.jpg)
Partitioning
![Page 23: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/23.jpg)
Double Check LockingUsed for lazy initialization
Photo by Kirainet. Some rights reserved.See: http://www.flickr.com/photos/torek/2467519466/
![Page 24: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/24.jpg)
Double Check LockingEasy to stuff up!!!
![Page 25: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/25.jpg)
Double Check LockingWas not possible in Java before 1.5.
Locks have become cheap, and so double-check not so useful.Usually better to use static initialization anyway.
Was unreliable in C++/Posix threads when I last looked.Find out if it works in your language/platform before using!!
![Page 26: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/26.jpg)
Double Check Locking
But it is a nice demonstration of some concurrency issues
![Page 27: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/27.jpg)
Double check locking(Warning: did not work before Java 1.5)
volatile Properties properties = null;final Object lock = new Object();
public Properties getProperties() { if (properties == null) { synchronized (lock) { if (properties == null) { tmp = new Properties(); tmp.load(inputStream); properties = tmp; } } } return properties;}
Possibly broken!Definitely Brittle!
![Page 28: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/28.jpg)
Lock-free Queue
![Page 29: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/29.jpg)
Lock Free Queue
Queue
Dummy Node
Real Head Node Tail
Penultimate node
Head
Tail: Might point to last or second last node!
References are all updated using the Atomic Test-And-Set.
![Page 30: Concurrency Patterns](https://reader034.fdocuments.net/reader034/viewer/2022051513/546b222daf79599b248b509c/html5/thumbnails/30.jpg)
References
• M. Ben-Ari, Principles of Concurrent Programming, Prentice Hall.
• Doug Lea, Concurrent Programming in Java Second Edition. Design Principles and Patterns, Addison-Wesley.
• Brian Goetz, Java Concurrency in Practice. Pearson Education.
• Software Engineering Radio, Episodes 12, 19, and 29. http://www.se-radio.net/