Java Concurrency
Towards a better parallel balanced life …
● By
● - Srinivasan.raghavan@oracle.com
- Vaibhav.x.choudhary@o...
Java Releases
JDK 1.5:
- Generics
-Concurrency
-Auto-boxing
J2SE 6:
- Collection Framework
enhancement
-Drag and Drop
-Imp...
Agenda
● Basics of concurrency
● Java Concurrent Package
● Reentanrant Lock / Performance
● Concurrent Collections
● Insig...
Basics of concurrency
● Amdhal's law
– Speedup <= 1/ (F + (1-F)/N)
● Age of multi-core processors and requirement
of paral...
General Design Principles

Its the mutable state stupid

Design for immutability

Make field final unless they need to ...
Java Concurrency Constructs
● In Java 5 a set of synchronisers was built giving
more power than intrinsic locking
● Atomic...
Intrinsic locks vs Reentarant Locks
● Intrinsic locks are very low level constructs
● Reentarant Locks offers more feature...
Dead Locks avoidance with try locks

Dead Lock can be avoided using the try locks
●
Try locks can used for Probabilistic ...
Condition Queues
●
Intrinsic locks can also work as condition queues
with wait and notify
●
Problem occurs when Intrinsic ...
Condition Queues
● Where a Lock replaces the use of synchronized
methods and statements, a Condition replaces
the use of t...
Performance
Can Reentarant lock replace synchronized?
– No , There are big issues when used carelessly
– Use only when Fai...
Java Synchronizers
● Executors
– Execution tasks in thread pools.
– Cyclic barrier ,Future task ,Semaphores
,Countdown lat...
Concurrent Collection
● Copy on write array List
● Concurrent hash-map
● Blocking queues and producer – consumer
pattern
Fork/Join Framework
● New parallel mechanism for compute intensive
tasks, the fork-join framework.
● Runs on work-stealing...
Fork/Join Framework
Java 8 way
Parallelism Built In
● Parallelism is the feature built in Java 8
● U can iterate,filter, sort ,map reduce an array in
par...
Java 8 Concurrency Changes
● Addition of fork-join common pool
● ConcurrentHashmapv8 which supports bulk
operations in par...
Concurrent Hashmap
● Started as a replacement to Hashtable.
● Implements Lock stripping
● Doesn’t throw concurrent modific...
Java concurrency
Upcoming SlideShare
Loading in...5
×

Java concurrency

207

Published on

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
207
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Java concurrency

  1. 1. Java Concurrency Towards a better parallel balanced life … ● By ● - Srinivasan.raghavan@oracle.com - Vaibhav.x.choudhary@oracle.com
  2. 2. Java Releases JDK 1.5: - Generics -Concurrency -Auto-boxing J2SE 6: - Collection Framework enhancement -Drag and Drop -Improve IO support J2SE 7: - Concurrency Utilities - Swing, -Fork/Join Pool , LWC&HWC - G1 J2SE 8: - Lambda expressions - Stream Package -More concurrency Updates
  3. 3. Agenda ● Basics of concurrency ● Java Concurrent Package ● Reentanrant Lock / Performance ● Concurrent Collections ● Insight of Fork/Join ● Concurrency in Java 8 ● Concurrent Collections java 8 updates
  4. 4. Basics of concurrency ● Amdhal's law – Speedup <= 1/ (F + (1-F)/N) ● Age of multi-core processors and requirement of parallel programming in mainstream. ● Writing concurrent programing is hard and writing correct concurrent program is even harder. ● Research in the field has been lead by Java designers and the concurrency interest group
  5. 5. General Design Principles  Its the mutable state stupid  Design for immutability  Make field final unless they need to be mutable  Encapsulate to manage complexity  Guard all variables in an invariant with same lock  Document the synchronization policy
  6. 6. Java Concurrency Constructs ● In Java 5 a set of synchronisers was built giving more power than intrinsic locking ● Atomic operations using Compare and swap ● Non-Blocking Collections
  7. 7. Intrinsic locks vs Reentarant Locks ● Intrinsic locks are very low level constructs ● Reentarant Locks offers more features ● Fair queuing ● TryLock ● Explicit Condition Queue
  8. 8. Dead Locks avoidance with try locks  Dead Lock can be avoided using the try locks ● Try locks can used for Probabilistic lock acquire of the lock and exponential back-off and spin lock ● Classic Transfer money Deadlock Problem Typical traffic problem.
  9. 9. Condition Queues ● Intrinsic locks can also work as condition queues with wait and notify ● Problem occurs when Intrinsic condition queue may be used with more than one condition predicate ● Reentarant locks provides explicit condition queues which offers more features ● Fairness in access of the lock which reduce contention ● Help us create queues per condition predicate
  10. 10. Condition Queues ● Where a Lock replaces the use of synchronized methods and statements, a Condition replaces the use of the Object monitor methods. ● Consider a case of bounded buffer – final Condition notFull = lock.newCondition(); – final Condition notEmpty = lock.newCondition(); ● ArrayBlockingQueue provides the functionality ● A typical consumer/producer problem with BlockingQueue.
  11. 11. Performance Can Reentarant lock replace synchronized? – No , There are big issues when used carelessly – Use only when Fair queuing, probabilistic locking and explicit condition queues are required
  12. 12. Java Synchronizers ● Executors – Execution tasks in thread pools. – Cyclic barrier ,Future task ,Semaphores ,Countdown latch ● Non-blocking algorithms – AtomicInteger – Compare and Swap (CAS) – Writing a correct non-blocking algorithm are tough. ● Find out the code.
  13. 13. Concurrent Collection ● Copy on write array List ● Concurrent hash-map ● Blocking queues and producer – consumer pattern
  14. 14. Fork/Join Framework ● New parallel mechanism for compute intensive tasks, the fork-join framework. ● Runs on work-stealing algorithm. ● Approach – Partition into subproblems. – Create subtaks. – Fork subtasks. – Join subtaks. – Compose solution.
  15. 15. Fork/Join Framework
  16. 16. Java 8 way
  17. 17. Parallelism Built In ● Parallelism is the feature built in Java 8 ● U can iterate,filter, sort ,map reduce an array in parallel ● The streams api makes heavy use of the Fork- Join framework ● The entire set of features offered by the streams api can be made to work in parallel by just asking for the parallel stream ● The inherit complexity of Fork-Join framework is handled under the hood
  18. 18. Java 8 Concurrency Changes ● Addition of fork-join common pool ● ConcurrentHashmapv8 which supports bulk operations in parallel ● Completable future ● Changes to the Future Task implementation
  19. 19. Concurrent Hashmap ● Started as a replacement to Hashtable. ● Implements Lock stripping ● Doesn’t throw concurrent modification exception ● Java 8 concurrenthashmap does a lot ● Built in support for parallelism ● More in the code .
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×