Concurrency
without Pain
in Pure Java
Palanivelrajan Balasubramanian <prajan@manh.com>
R & D, Architecture
12-02-2013
About Me
-
Joined Manhattan Associates in 2006
-
Member of R&D Architecture
- Java Developer
- Spring
- Hibernate
- Application Servers
- Scripting
-
Current Project - System Director 3.0
Agenda
- Bank Account System
-
Problems with Synchronization
- Introduction to Software Transactional Memory
- Clojure
- Akka
- Discussion
DISCLAIMER
-
I am not a expert in this topic
-
Developed interest after attending "Concurrency without pain in pure java" session by Venkat Subramaniam
- If i am not sure about your question, i will go back do my research and will get back to you.
Bank Account System
Lets use the java 'synchronization' and develop a bank account system
Problems with Java Synchronization
-
No errors/warning from JDK
-
Programmers can get synchronization wrong
- Too conservative - Back to single threaded execution
- Thread Contention - waiting for lock
- Deadlock - Waiting infinitely for locks
- Conflating Identity with State
Conflating Identity with State
-
When we create an object (Employee) in java
- we receive a pointer to the instance that serves as both
- its identity - Object represents Palanivel , Employee
- State - Object also hold info about my start data July 2006
- Anyone having access to the object can change the start date
- Previous start date is lost forever
- A thread with a pointer to Employee Palanivel has to assume that its value can change at any moment - So we add 'synchronization'
Software TRANSACTIONal Memory
-
Identity and state are managed separately
-
Identity - a stable logical entity associated with a series of different values over time
- value - something that doesn't change. All values are immutable
- The date May 03, 1985 never change
- You might have an identity called “today”
- At one point, “today” was associated with “May 03, 1985”
-
The next day, it was associated with “May 04, 1985” and now that identity is associated with “Dec 02, 2013”
STM
-
The identity is ALWAYS associated with a single immutable value at a given time; someone (a thread) can request that it be associated with a different immutable value (perhaps creating the new value based on the old value)
- the association is then changed, not the values
-
This immutability is good in concurrent situations, since there is never any danger of a value changing out from under you
STM BENEFITS
-
separating identity from state
-
lock-free programming
-
Keep tracks of mutable identity and screams if its modified outside of a transaction
- Multiple threads can read this value with no contention
clojure
-
Clojure is a dynamic programming language
-
Implements STM
- Can be used in Java apps by including the library to classpath
- http://clojure.org/
Using Clojure in our BANK ACCOUNT SYSTEM
Akka
-
Actor based system supporting STM
-
Written in Scala, but also in Java as well
-
Highly scalable
- Akka's unit of code organization is called an Actor
- Like Java EE servlets and session beans
- Runs on a dedicated threads, managed by Akka framework
- Never think about threads, locks
- Distributed by Design
- Actors can run on local or can be run on a remote machine, producing the same result