Upload
fuqiang-wang
View
2.587
Download
0
Tags:
Embed Size (px)
DESCRIPTION
more choices for java's default share-state concurrency modelthe doc is incomplete
Citation preview
Darren.Wang(王福强)
More Than Java Concurrency
Thursday, January 20, 2011
Agenda
More Than Java Concurrency? What’s More?
Several Concurrency Models/Patterns
Case Study
Questions?
The end
Thursday, January 20, 2011
Why concurrency?
为了反映一些问题的逻辑结构
为了应对相互独立的多台物理设备
为了同时在多部处理器上运行以提高性能
Thursday, January 20, 2011
What concurrency offers
reduce latency
divide and conquer, run in parallel reduce the latency
hide latency
asynchronous
increase throughput
more tasks can be run without wasting available computing power
Thursday, January 20, 2011
What we do with Java Concurrency
State, That’s all of the about.
Atomicity - keep state intact
Visibility - make right state access
Thursday, January 20, 2011
When Things Go Wrong
Thursday, January 20, 2011
What If ...
Thursday, January 20, 2011
What We Will Talk Today
Share-State Concurrency
Share-Nothing Concurrency
Software Transaction Memory
Data-Flow Concurrency
Thursday, January 20, 2011
Share-State Concurrency
Thursday, January 20, 2011
JMM Simplified
Picture borrowed from <<Java多线程设计模式>>
Thursday, January 20, 2011
JMM escalation
Thursday, January 20, 2011
How to deal with?
How many ways can you count?
Here we go...
Thursday, January 20, 2011
Immutability
immutable anytime
Immutable Object
immutable before publishing
Map & ConcurrencyMap
(1)To Find More in book <<Java Concurrency In Practice>>
Thursday, January 20, 2011
Confinement
Thread Confinement
Thread-Specific Variables(ThreadLocal)
Stack Confinement
Method Stack Scope
Thursday, January 20, 2011
Synchronization
Synchronized Block(Implicit Lock)
Explicit Lock
Volatile (Partial Synchronization Support)
Thursday, January 20, 2011
Gotchas With Synchronization
Deadlock
how to prevent it?
Live lock
Starvation
Thursday, January 20, 2011
Gotchas With Synchronization
Deadlock
how to prevent it?
Live lock
Starvation
Resource Re-ordering
Thursday, January 20, 2011
Gotchas With Synchronization
Deadlock
how to prevent it?
Live lock
Starvation
Resource Re-ordering
Periodic Retry
Thursday, January 20, 2011
Performance Tuning with Synchronization
reduce the time of holding locks
break global locks into fine-grained locks
what coming next?
Thursday, January 20, 2011
CAS
Compare-And-Set | Compare-And-Swap
java.util.concurrent.atomic after Java5
AtomicInteger
AtomicLong
AtomicBoolean
AtomicReference
Thursday, January 20, 2011
Section Conclusion
Pessimistic Model
Lock-Based
Optimistic Model
Lock-Free
Thursday, January 20, 2011
Share-Nothing Concurrency
Thursday, January 20, 2011
Share-Nothing Concurrency
AKA. Message Passing Concurrency
Confinement In a Big Picture
Thursday, January 20, 2011
Actor Model
First proposed by Carl Hewitt in 1973
improved by Gul Agha
An Actor Can:
change internal state
send messages to peer actors
create new actors
migrate to another computing host
Thursday, January 20, 2011
Actor Rocks
more light weight
event based actors of akka can create 6.5million on 4 G RAM (600 bytes each)
much easier, just send messages
counter ! tick
Thursday, January 20, 2011
Actors Available
Erlang Platform
Scala Platform
TBA, EBA
Lift Actor, Akka Actor
Java Platform
Kilim, SALSA(simple actor language, system and architecture)
Other
Thursday, January 20, 2011
Actor Almighty?
when we really have shared states, e.g. bank account?
when we need to form unified consensus
when we need synchronous behavior
a bit verbose compared to method dispatch
Thursday, January 20, 2011
Section Conclusion
Actor Model Rocks In Some Situations, But not all
Share Nothing Simplify the Architecture Design
Prefer Share Nothing to Share State If Possible
Thursday, January 20, 2011
You Know Lego, Right?
Thursday, January 20, 2011
Case Study I
Session Management
Centralized Storage
memcached, database...
Client-Specific Storage
secure cookie
other local-storage mechanism
Thursday, January 20, 2011
Case Study II
Map-Reduce
When Share Nothing can rock in
When Share State Is a Must
Thursday, January 20, 2011
Case Study III
Symmetric Cluster
Share Nothing
Asymmetric Cluster
Share State
Thursday, January 20, 2011
STM
Software Transaction Memory
Transaction operations execute in the own thread in parallel, commit will cause One to be successful and others to abort or retry
Thursday, January 20, 2011
Sounds Familiar?
Optimistic Model
Can be nested
Simple Programming Model
Thursday, January 20, 2011
Available Solutions
Clojure
Concurrent Haskell
STM .Net
More...
Thursday, January 20, 2011
Cons
All operations in scope of a transaction:
need to be idempotent
can’t have side-effects
Memory Concern
Conflicting Rate Concern
Thursday, January 20, 2011
Section Conclusion
optimistic model
simple programming model
with restrictions
which may cause limitations(MS drops STM)
Thursday, January 20, 2011
Data-Flow Concurrency
AKA. Declarative Concurrency
Thursday, January 20, 2011
Have you ever...
Thursday, January 20, 2011
Features
Single Assignment Variables
Data-States trigger process flow
Thursday, January 20, 2011
Available Languages
LabView图形化编程语言
Thursday, January 20, 2011
Usage Scenarios
Signal Process System
Thursday, January 20, 2011
Typical Operations
create a data-flow variable
wait for the variable to be bound
bind the variable
Thursday, January 20, 2011
Limitations
Can’t have side-effects
Exceptions
IO
Others
Thursday, January 20, 2011
Section Conclusion
Data Based Concurrency
Thursday, January 20, 2011
comparison of common facilities
Executor (Agent)
Actor (HawtDispatch)
STM
Transactor(Actor + STM)
Thursday, January 20, 2011
Thursday, January 20, 2011
Thank You
Thursday, January 20, 2011
The End
Thursday, January 20, 2011