SlideShare a Scribd company logo
1 of 59
The Well-Grounded Java Developer

    Modern Java Concurrency
Are you a fluffy animal lover..?
         Leave now.



                              2
Why Modern Java Concurrency?
 • The WGJD wants to utilise modern hardware


 • The WGJD wants to write concurrent code
    - Safely
    - Without fear
    - With an understanding of performance implications


 • The WGJD wants to take advantage of:
    - JVM support for parallelised operations
    - An API that expands on synchronized


 • Modern Java Concurrency lets you do all of this
About this section
• We only have ~60mins hour to talk today


• Huge amount to talk about


• This subject typically fills 4 days worth of training


• We will give you some highlights today


• For die-hard low latency fiends
   - Locks are actually bad hmmkay!
   - Come talk to us afterwards to find out more
Modern Java concurrency
• Not a new subject
    - Underwent a revolution with Java 5
    - More refinements in 6 and 7
    - Another major library overhaul coming in 8 with lambdas


• java.util.concurrent (j.u.c) really fast in 6
    - Better yet in 7 & blazing in 8


• j.u.c. is still under-appreciated
    - Too much Java 4-style concurrency code still written
Java concurrency - Why not upgrade?
   • Too much legacy code?
      - People scared to refactor?


   • People don’t know j.u.c is easier?
      - People don’t know j.u.c is faster than classic?


   • People don’t know that you can mix-and-match
      - With a bit of care


   • Still not being taught at Universities?


   • Not enough people reading Doug Lea or Brian Goetz?
Perhaps... previous treatments
 didn't involve enough Otters.


     We will rectify this.

                             7
If you suffer from lutraphobia, you
      may want to leave now...


    Fluffy cute animals BITE

                               8
Otterly Amazing Tails of Modern Java
           Concurrency




   • Srsly
      - Otters! See those teeth? We warned you.
Why Otters?
• Otters are a very good metaphor
   - Not just because they look a bit
     like threads (i.e. long and thin)


• They are Collaborative,
 Competitive & Sneaky


• Hare off in opposite directions
   - Wreaking havoc if not contained
Otter Management (aka the 4 forces)
  • Safety
     - Does each object stay self-consistent?
     - No matter what other operations are happening?


  • Liveness
     - Does the program eventually progress?
     - Are any failures to progress temporary or permanent?


  • Performance
     - How well does the system take advantage of cores?


  • Reusability
     - How easy is it to reuse the system in other applications?
Some History
• Until recently, most CPUs had one processing core


• Multithreading was simulated on that single core
   - Not true concurrency


• Serial approach to algorithms often sufficed


• Interleaved multithreading can mask errors
   - Or be more forgiving than true concurrency


• Why, how and when did things change?
Moore’s Law
• “The number of transistors on an economic-to-
  produce chip roughly doubles every 2 years”


• Originally stated in 1965
   - Expected to hold for the 10 years to 1975
   - Still going strong


• Named for Gordon Moore (Intel founder)
   - About # transistors, not clock speed or overall performance
Transistor Counts
Transistor Counts
Moore’s Law - Problems
• Remember, it's not about overall performance


• Memory latency exponent gap
   - Need to keep the processing pipeline full
   - Add memory caches of faster SRAM “close” to the CPU
       • (L1, L2 etc)


• Code is restricted by L1 cache misses
   - Rather than CPU speed
   - After JIT compilation
Spending the transistor budget




• More and more complex contortions...
   - ILP, CMT, Branch prediction, etc, etc
Multi-core
• If we can’t increase clock speed / performance..
   - We have to go multi-core
   - Concurrency and performance are now tied together


• Real concurrency
   - Separate threads executing on cores at the same moment


• The JVM runtime controls thread scheduling
   - Java scheduling does NOT behave like OS scheduling


• Concurrency becomes the performance improver
Classic Java Concurrency
Classic Java Concurrency




• Why synchronized?


• Provides exclusion


• Need locking to make mutation concurrency-safe


• Locking gets complicated
   - Can become fragile
I wrap sychronized....
around entire classes


  Safe as Fort Knox
                         21
3 approaches to Concurrent Type Safety
    • Fully-synchronized Objects
       - Synchronize all methods on all classes


    • Immutability
       - Useful, but may have high copy-cost
       - Requires programmer discipline


    • Be Very, Very Careful
       - Difficult
       - Fragile
       - With Java - Often the only game in town
The JMM
• Mathematical description of memory


• Most impenetrable part of the Java language spec
   - Even worse than generics


• JMM makes minimum guarantees


• Real JVMs (and CPUs) may do more
   - Especially Intel chipsets
The JMM




• Primary concepts:
   -   synchronizes-with
   -   happens-before
   -   release-before-acquire
   -   as-if-serial
Synchronizes-with
Synchronizes-with
• Threads have their own desc of an object’s state
   - This must be flushed to main memory and other threads


• synchronized means that this local view:
   - Has been synchronized-with the other threads


• Defines touch-points where threads must perform
  synching
java.util.concurrent
java.util.concurrent

• Thanks, Doug Lea and co!


• j.u.c has building blocks for concurrent code
   -   ReentrantLock
   -   Condition
   -   ConcurrentHashMap
   -   CopyOnWriteArrayList
   -   Other Concurrent Data Structures
Locks in j.u.c




• Lock is an interface


• ReentrantLock is the usual implementation
Conditions in j.u.c
Conditions in j.u.c
HashMap




• HashMap has:
   - Hash function
   - Even distribution of buckets
ConcurrentHashMap (CHM)




• Concurrent form
   - Can lock independently
   - Seems lock-free to users
   - Has atomic operations
CopyOnWriteArrayList (COWAL)


•
CopyOnWriteArrayList (COWAL)
• Makes separate copies of underlying structure


• Iterator will never throw
 ConcurrentModificationException
CountDownLatch
CountDownLatch
• A group consensus construct


• countDown() decrements the count


• await() blocks until count == 0
   - i.e. consensus


• Constructor takes an int (the count)


• Quite a few use cases
   - e.g. Multithreaded testing
Example - CountDownLatch
Handoff Queue (in-mem)
Handoff Queue (in-mem)
• Efficient way to hand off work between threadpools


• BlockingQueue a good pick


• Has blocking ops with timeouts
   - e.g. for backoff / retry


• Two basic implementations
   - ArrayList and LinkedList backed


• Java 7 introduces the shiny new TransferQueue
Just use JMS!




                41
Example - LinkedBlockingQueue
Executors
Animals were harmed in the
 making of this presentation


    Crab got executed

                           44
Executors
• j.u.c execution constructs
   - Callable, Future, FutureTask


• In addition to the venerable
   - Thread and Runnable


• Stop using TimerTask!


• Executors class provides factory methods for
  making threadpools
   - ScheduledThreadPoolExecutor is one standard choice
Example - ThreadPoolManager
Example - QueueReaderTask
Fork/Join
Fork/Join
• Java 7 introduces F/J
   - similar to MapReduce
   - useful for a certain class of problems
   - F/J executions are not really threads


• In our example, we subclass RecursiveAction


• Need to provide a compute() method
   - And a way of merging results


• F/J provides an invokeAll() to hand off more tasks
Fork/Join




• Typical divide and conquer style problem
   - invokeall() performs the threadpool, worker & queue
     magic
Concurrent Java Code
• Mutable state (objects) protected by locks


• Concurrent data structures
   - CHM, COWAL


• Be careful of performance
   - especially COWAL


• Explicit synchronization for multi-threading


• Executor-based threadpools


• Queue-like handoffs used for asynch comms
Stepping Back
Stepping Back
• Concurrency is key to the future of performant code


• Mutable state is hard


• Need both synch & asynch state sharing


• Locks can be hard to use correctly


• JMM is a low-level, flexible model
   - Need higher-level concurrency model
   - Thread is still too low-level
Imagine a world...
• The JVM helped out the programmer more:
   -   Runtime-managed concurrency
   -   Collections were thread-safe by default
   -   Objects were immutable by default
   -   State was well encapsulated and not shared by default


• Thread wasn’t the default choice for unit of
  concurrent execution


• Copy-on-write was the basis for mutation of
  collections / synchronous multithreading


• Hand-off queues were the basis for asynchronous
  multithreading
What can we do with Java?
• We’re stuck with a lot of heritage in Java
   - But the JVM and JMM are very sound


• You don’t have to abandon Java
   - Mechanical sympathy and clean code get you far
   - The JIT compiler just gets better and better


• If we wanted to dream of a new language
   - It should be on the JVM
   - It should build on what we’ve learned in 15 years of Java
New Frontiers in Concurrency
• There are several options now on the JVM
   - New possibilities built-in to the language syntax
   - Synch and asynch models


• Scala offers an Actors model
   - And the powerful Akka framework


• Clojure is immutable by default
   - Has agents (like actors) & shared-nothing by default
   - Also has a Software Transactional Memory (STM) model


• Groovy has GPARs
Acknowledgments
• All Otter images Creative Commons or Fair Use


• Matt Raible
   - 20 criteria of selecting web frameworks


• Ola Bini
   - Polyglot pyramid


• Photos owned by Flickr Users
   - moff, spark, sodaro, lonecellotheory, tomsowerby
   - farnsworth, prince, marcus_jb1973, mliu92, Ed Zitron,
   - NaturalLight & monkeywing



• Dancing Otter by the amazing Nicola Slater @ folksy
Where is our beer!?




                      58
Thanks for listening! (@kittylyst,
          @karianna)

More Related Content

What's hot

Introduction to Java 7 (Devoxx Nov/2011)
Introduction to Java 7 (Devoxx Nov/2011)Introduction to Java 7 (Devoxx Nov/2011)
Introduction to Java 7 (Devoxx Nov/2011)Martijn Verburg
 
Perl On The JVM (London.pm Talk 2009-04)
Perl On The JVM (London.pm Talk 2009-04)Perl On The JVM (London.pm Talk 2009-04)
Perl On The JVM (London.pm Talk 2009-04)Ben Evans
 
Paperwork, Politics and Pain - Our year in the JCP (FOSDEM 2012)
Paperwork, Politics and Pain - Our year in the JCP (FOSDEM 2012)Paperwork, Politics and Pain - Our year in the JCP (FOSDEM 2012)
Paperwork, Politics and Pain - Our year in the JCP (FOSDEM 2012)Martijn Verburg
 
A tour of Java and the JVM
A tour of Java and the JVMA tour of Java and the JVM
A tour of Java and the JVMAlex Birch
 
The Economies of Scaling Software
The Economies of Scaling SoftwareThe Economies of Scaling Software
The Economies of Scaling SoftwareAbdelmonaim Remani
 
PHP Frameworks Review - Mar 19 2015
PHP Frameworks Review - Mar 19 2015PHP Frameworks Review - Mar 19 2015
PHP Frameworks Review - Mar 19 2015kyphpug
 
Java Presentation
Java PresentationJava Presentation
Java PresentationAmr Salah
 
Introduction to Java
Introduction to Java Introduction to Java
Introduction to Java RatnaJava
 
Introduction to Java Part-2
Introduction to Java Part-2Introduction to Java Part-2
Introduction to Java Part-2RatnaJava
 
Java 8 selected updates
Java 8 selected updatesJava 8 selected updates
Java 8 selected updatesVinay H G
 
The State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila SzegediThe State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila SzegediZeroTurnaround
 
Polyglot Plugin Programming
Polyglot Plugin ProgrammingPolyglot Plugin Programming
Polyglot Plugin ProgrammingAtlassian
 
Kitware: Qt and Scientific Computing
Kitware: Qt and Scientific ComputingKitware: Qt and Scientific Computing
Kitware: Qt and Scientific Computingaccount inactive
 
Go - A Key Language in Enterprise Application Development?
Go - A Key Language in Enterprise Application Development?Go - A Key Language in Enterprise Application Development?
Go - A Key Language in Enterprise Application Development?C4Media
 
Java Edge.2009.Grails.Web.Dev.Made.Easy
Java Edge.2009.Grails.Web.Dev.Made.EasyJava Edge.2009.Grails.Web.Dev.Made.Easy
Java Edge.2009.Grails.Web.Dev.Made.Easyroialdaag
 
Building Atlassian Plugins with Groovy - Atlassian Summit 2010 - Lightning Talks
Building Atlassian Plugins with Groovy - Atlassian Summit 2010 - Lightning TalksBuilding Atlassian Plugins with Groovy - Atlassian Summit 2010 - Lightning Talks
Building Atlassian Plugins with Groovy - Atlassian Summit 2010 - Lightning TalksAtlassian
 
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...Miles Sabin
 
Smalltalk and ruby - 2012-12-08
Smalltalk and ruby  - 2012-12-08Smalltalk and ruby  - 2012-12-08
Smalltalk and ruby - 2012-12-08Koan-Sin Tan
 
Rubykaigi 2017-nishimotz-v6
Rubykaigi 2017-nishimotz-v6Rubykaigi 2017-nishimotz-v6
Rubykaigi 2017-nishimotz-v6Takuya Nishimoto
 

What's hot (20)

Introduction to Java 7 (Devoxx Nov/2011)
Introduction to Java 7 (Devoxx Nov/2011)Introduction to Java 7 (Devoxx Nov/2011)
Introduction to Java 7 (Devoxx Nov/2011)
 
Perl On The JVM (London.pm Talk 2009-04)
Perl On The JVM (London.pm Talk 2009-04)Perl On The JVM (London.pm Talk 2009-04)
Perl On The JVM (London.pm Talk 2009-04)
 
Paperwork, Politics and Pain - Our year in the JCP (FOSDEM 2012)
Paperwork, Politics and Pain - Our year in the JCP (FOSDEM 2012)Paperwork, Politics and Pain - Our year in the JCP (FOSDEM 2012)
Paperwork, Politics and Pain - Our year in the JCP (FOSDEM 2012)
 
A tour of Java and the JVM
A tour of Java and the JVMA tour of Java and the JVM
A tour of Java and the JVM
 
The Economies of Scaling Software
The Economies of Scaling SoftwareThe Economies of Scaling Software
The Economies of Scaling Software
 
PHP Frameworks Review - Mar 19 2015
PHP Frameworks Review - Mar 19 2015PHP Frameworks Review - Mar 19 2015
PHP Frameworks Review - Mar 19 2015
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
Introduction to Java
Introduction to Java Introduction to Java
Introduction to Java
 
Introduction to Java Part-2
Introduction to Java Part-2Introduction to Java Part-2
Introduction to Java Part-2
 
History of java
History of javaHistory of java
History of java
 
Java 8 selected updates
Java 8 selected updatesJava 8 selected updates
Java 8 selected updates
 
The State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila SzegediThe State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila Szegedi
 
Polyglot Plugin Programming
Polyglot Plugin ProgrammingPolyglot Plugin Programming
Polyglot Plugin Programming
 
Kitware: Qt and Scientific Computing
Kitware: Qt and Scientific ComputingKitware: Qt and Scientific Computing
Kitware: Qt and Scientific Computing
 
Go - A Key Language in Enterprise Application Development?
Go - A Key Language in Enterprise Application Development?Go - A Key Language in Enterprise Application Development?
Go - A Key Language in Enterprise Application Development?
 
Java Edge.2009.Grails.Web.Dev.Made.Easy
Java Edge.2009.Grails.Web.Dev.Made.EasyJava Edge.2009.Grails.Web.Dev.Made.Easy
Java Edge.2009.Grails.Web.Dev.Made.Easy
 
Building Atlassian Plugins with Groovy - Atlassian Summit 2010 - Lightning Talks
Building Atlassian Plugins with Groovy - Atlassian Summit 2010 - Lightning TalksBuilding Atlassian Plugins with Groovy - Atlassian Summit 2010 - Lightning Talks
Building Atlassian Plugins with Groovy - Atlassian Summit 2010 - Lightning Talks
 
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
 
Smalltalk and ruby - 2012-12-08
Smalltalk and ruby  - 2012-12-08Smalltalk and ruby  - 2012-12-08
Smalltalk and ruby - 2012-12-08
 
Rubykaigi 2017-nishimotz-v6
Rubykaigi 2017-nishimotz-v6Rubykaigi 2017-nishimotz-v6
Rubykaigi 2017-nishimotz-v6
 

Similar to Modern Java Concurrency Highlights

Modern Java Concurrency (Devoxx Nov/2011)
Modern Java Concurrency (Devoxx Nov/2011)Modern Java Concurrency (Devoxx Nov/2011)
Modern Java Concurrency (Devoxx Nov/2011)Martijn Verburg
 
Modern Java Concurrency
Modern Java ConcurrencyModern Java Concurrency
Modern Java ConcurrencyBen Evans
 
Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Haim Yadid
 
Java in High Frequency Trading
Java in High Frequency TradingJava in High Frequency Trading
Java in High Frequency TradingViktor Sovietov
 
Writing Scalable Software in Java
Writing Scalable Software in JavaWriting Scalable Software in Java
Writing Scalable Software in JavaRuben Badaró
 
What’s expected in Java 9
What’s expected in Java 9What’s expected in Java 9
What’s expected in Java 9Gal Marder
 
Actors and Threads
Actors and ThreadsActors and Threads
Actors and Threadsmperham
 
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben EvansJava Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben EvansJAX London
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in JavaLakshmi Narasimhan
 
Composable Futures with Akka 2.0
Composable Futures with Akka 2.0Composable Futures with Akka 2.0
Composable Futures with Akka 2.0Mike Slinn
 
Clojure in real life 17.10.2014
Clojure in real life 17.10.2014Clojure in real life 17.10.2014
Clojure in real life 17.10.2014Metosin Oy
 
Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014Charles Nutter
 
Scalability, Availability & Stability Patterns
Scalability, Availability & Stability PatternsScalability, Availability & Stability Patterns
Scalability, Availability & Stability PatternsJonas Bonér
 

Similar to Modern Java Concurrency Highlights (20)

Modern Java Concurrency (Devoxx Nov/2011)
Modern Java Concurrency (Devoxx Nov/2011)Modern Java Concurrency (Devoxx Nov/2011)
Modern Java Concurrency (Devoxx Nov/2011)
 
Modern Java Concurrency
Modern Java ConcurrencyModern Java Concurrency
Modern Java Concurrency
 
Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014
 
Java in High Frequency Trading
Java in High Frequency TradingJava in High Frequency Trading
Java in High Frequency Trading
 
Writing Scalable Software in Java
Writing Scalable Software in JavaWriting Scalable Software in Java
Writing Scalable Software in Java
 
What’s expected in Java 9
What’s expected in Java 9What’s expected in Java 9
What’s expected in Java 9
 
Actors and Threads
Actors and ThreadsActors and Threads
Actors and Threads
 
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben EvansJava Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
 
Java and the JVM
Java and the JVMJava and the JVM
Java and the JVM
 
Concurrency in Java
Concurrency in JavaConcurrency in Java
Concurrency in Java
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
 
Composable Futures with Akka 2.0
Composable Futures with Akka 2.0Composable Futures with Akka 2.0
Composable Futures with Akka 2.0
 
Clojure in real life 17.10.2014
Clojure in real life 17.10.2014Clojure in real life 17.10.2014
Clojure in real life 17.10.2014
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014
 
Scalability, Availability & Stability Patterns
Scalability, Availability & Stability PatternsScalability, Availability & Stability Patterns
Scalability, Availability & Stability Patterns
 
Thread
ThreadThread
Thread
 
Thread
ThreadThread
Thread
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
gcdtmp
gcdtmpgcdtmp
gcdtmp
 

More from Martijn Verburg

Garbage Collection - The Useful Parts
Garbage Collection - The Useful PartsGarbage Collection - The Useful Parts
Garbage Collection - The Useful PartsMartijn Verburg
 
Modern software development anti patterns (OSCON 2012)
Modern software development anti patterns (OSCON 2012)Modern software development anti patterns (OSCON 2012)
Modern software development anti patterns (OSCON 2012)Martijn Verburg
 
Back to the future with Java 7 (Geekout June/2011)
Back to the future with Java 7 (Geekout June/2011)Back to the future with Java 7 (Geekout June/2011)
Back to the future with Java 7 (Geekout June/2011)Martijn Verburg
 
How to open source a project at Mega Corp (Geecon - May/2011)
How to open source a project at Mega Corp (Geecon - May/2011)How to open source a project at Mega Corp (Geecon - May/2011)
How to open source a project at Mega Corp (Geecon - May/2011)Martijn Verburg
 
Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2Martijn Verburg
 

More from Martijn Verburg (6)

NoHR Hiring
NoHR HiringNoHR Hiring
NoHR Hiring
 
Garbage Collection - The Useful Parts
Garbage Collection - The Useful PartsGarbage Collection - The Useful Parts
Garbage Collection - The Useful Parts
 
Modern software development anti patterns (OSCON 2012)
Modern software development anti patterns (OSCON 2012)Modern software development anti patterns (OSCON 2012)
Modern software development anti patterns (OSCON 2012)
 
Back to the future with Java 7 (Geekout June/2011)
Back to the future with Java 7 (Geekout June/2011)Back to the future with Java 7 (Geekout June/2011)
Back to the future with Java 7 (Geekout June/2011)
 
How to open source a project at Mega Corp (Geecon - May/2011)
How to open source a project at Mega Corp (Geecon - May/2011)How to open source a project at Mega Corp (Geecon - May/2011)
How to open source a project at Mega Corp (Geecon - May/2011)
 
Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2
 

Recently uploaded

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Recently uploaded (20)

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

Modern Java Concurrency Highlights

  • 1. The Well-Grounded Java Developer Modern Java Concurrency
  • 2. Are you a fluffy animal lover..? Leave now. 2
  • 3. Why Modern Java Concurrency? • The WGJD wants to utilise modern hardware • The WGJD wants to write concurrent code - Safely - Without fear - With an understanding of performance implications • The WGJD wants to take advantage of: - JVM support for parallelised operations - An API that expands on synchronized • Modern Java Concurrency lets you do all of this
  • 4. About this section • We only have ~60mins hour to talk today • Huge amount to talk about • This subject typically fills 4 days worth of training • We will give you some highlights today • For die-hard low latency fiends - Locks are actually bad hmmkay! - Come talk to us afterwards to find out more
  • 5. Modern Java concurrency • Not a new subject - Underwent a revolution with Java 5 - More refinements in 6 and 7 - Another major library overhaul coming in 8 with lambdas • java.util.concurrent (j.u.c) really fast in 6 - Better yet in 7 & blazing in 8 • j.u.c. is still under-appreciated - Too much Java 4-style concurrency code still written
  • 6. Java concurrency - Why not upgrade? • Too much legacy code? - People scared to refactor? • People don’t know j.u.c is easier? - People don’t know j.u.c is faster than classic? • People don’t know that you can mix-and-match - With a bit of care • Still not being taught at Universities? • Not enough people reading Doug Lea or Brian Goetz?
  • 7. Perhaps... previous treatments didn't involve enough Otters. We will rectify this. 7
  • 8. If you suffer from lutraphobia, you may want to leave now... Fluffy cute animals BITE 8
  • 9. Otterly Amazing Tails of Modern Java Concurrency • Srsly - Otters! See those teeth? We warned you.
  • 10. Why Otters? • Otters are a very good metaphor - Not just because they look a bit like threads (i.e. long and thin) • They are Collaborative, Competitive & Sneaky • Hare off in opposite directions - Wreaking havoc if not contained
  • 11. Otter Management (aka the 4 forces) • Safety - Does each object stay self-consistent? - No matter what other operations are happening? • Liveness - Does the program eventually progress? - Are any failures to progress temporary or permanent? • Performance - How well does the system take advantage of cores? • Reusability - How easy is it to reuse the system in other applications?
  • 12. Some History • Until recently, most CPUs had one processing core • Multithreading was simulated on that single core - Not true concurrency • Serial approach to algorithms often sufficed • Interleaved multithreading can mask errors - Or be more forgiving than true concurrency • Why, how and when did things change?
  • 13. Moore’s Law • “The number of transistors on an economic-to- produce chip roughly doubles every 2 years” • Originally stated in 1965 - Expected to hold for the 10 years to 1975 - Still going strong • Named for Gordon Moore (Intel founder) - About # transistors, not clock speed or overall performance
  • 16. Moore’s Law - Problems • Remember, it's not about overall performance • Memory latency exponent gap - Need to keep the processing pipeline full - Add memory caches of faster SRAM “close” to the CPU • (L1, L2 etc) • Code is restricted by L1 cache misses - Rather than CPU speed - After JIT compilation
  • 17. Spending the transistor budget • More and more complex contortions... - ILP, CMT, Branch prediction, etc, etc
  • 18. Multi-core • If we can’t increase clock speed / performance.. - We have to go multi-core - Concurrency and performance are now tied together • Real concurrency - Separate threads executing on cores at the same moment • The JVM runtime controls thread scheduling - Java scheduling does NOT behave like OS scheduling • Concurrency becomes the performance improver
  • 20. Classic Java Concurrency • Why synchronized? • Provides exclusion • Need locking to make mutation concurrency-safe • Locking gets complicated - Can become fragile
  • 21. I wrap sychronized.... around entire classes Safe as Fort Knox 21
  • 22. 3 approaches to Concurrent Type Safety • Fully-synchronized Objects - Synchronize all methods on all classes • Immutability - Useful, but may have high copy-cost - Requires programmer discipline • Be Very, Very Careful - Difficult - Fragile - With Java - Often the only game in town
  • 23. The JMM • Mathematical description of memory • Most impenetrable part of the Java language spec - Even worse than generics • JMM makes minimum guarantees • Real JVMs (and CPUs) may do more - Especially Intel chipsets
  • 24. The JMM • Primary concepts: - synchronizes-with - happens-before - release-before-acquire - as-if-serial
  • 26. Synchronizes-with • Threads have their own desc of an object’s state - This must be flushed to main memory and other threads • synchronized means that this local view: - Has been synchronized-with the other threads • Defines touch-points where threads must perform synching
  • 28. java.util.concurrent • Thanks, Doug Lea and co! • j.u.c has building blocks for concurrent code - ReentrantLock - Condition - ConcurrentHashMap - CopyOnWriteArrayList - Other Concurrent Data Structures
  • 29. Locks in j.u.c • Lock is an interface • ReentrantLock is the usual implementation
  • 32. HashMap • HashMap has: - Hash function - Even distribution of buckets
  • 33. ConcurrentHashMap (CHM) • Concurrent form - Can lock independently - Seems lock-free to users - Has atomic operations
  • 35. CopyOnWriteArrayList (COWAL) • Makes separate copies of underlying structure • Iterator will never throw ConcurrentModificationException
  • 37. CountDownLatch • A group consensus construct • countDown() decrements the count • await() blocks until count == 0 - i.e. consensus • Constructor takes an int (the count) • Quite a few use cases - e.g. Multithreaded testing
  • 40. Handoff Queue (in-mem) • Efficient way to hand off work between threadpools • BlockingQueue a good pick • Has blocking ops with timeouts - e.g. for backoff / retry • Two basic implementations - ArrayList and LinkedList backed • Java 7 introduces the shiny new TransferQueue
  • 44. Animals were harmed in the making of this presentation Crab got executed 44
  • 45. Executors • j.u.c execution constructs - Callable, Future, FutureTask • In addition to the venerable - Thread and Runnable • Stop using TimerTask! • Executors class provides factory methods for making threadpools - ScheduledThreadPoolExecutor is one standard choice
  • 49. Fork/Join • Java 7 introduces F/J - similar to MapReduce - useful for a certain class of problems - F/J executions are not really threads • In our example, we subclass RecursiveAction • Need to provide a compute() method - And a way of merging results • F/J provides an invokeAll() to hand off more tasks
  • 50. Fork/Join • Typical divide and conquer style problem - invokeall() performs the threadpool, worker & queue magic
  • 51. Concurrent Java Code • Mutable state (objects) protected by locks • Concurrent data structures - CHM, COWAL • Be careful of performance - especially COWAL • Explicit synchronization for multi-threading • Executor-based threadpools • Queue-like handoffs used for asynch comms
  • 53. Stepping Back • Concurrency is key to the future of performant code • Mutable state is hard • Need both synch & asynch state sharing • Locks can be hard to use correctly • JMM is a low-level, flexible model - Need higher-level concurrency model - Thread is still too low-level
  • 54. Imagine a world... • The JVM helped out the programmer more: - Runtime-managed concurrency - Collections were thread-safe by default - Objects were immutable by default - State was well encapsulated and not shared by default • Thread wasn’t the default choice for unit of concurrent execution • Copy-on-write was the basis for mutation of collections / synchronous multithreading • Hand-off queues were the basis for asynchronous multithreading
  • 55. What can we do with Java? • We’re stuck with a lot of heritage in Java - But the JVM and JMM are very sound • You don’t have to abandon Java - Mechanical sympathy and clean code get you far - The JIT compiler just gets better and better • If we wanted to dream of a new language - It should be on the JVM - It should build on what we’ve learned in 15 years of Java
  • 56. New Frontiers in Concurrency • There are several options now on the JVM - New possibilities built-in to the language syntax - Synch and asynch models • Scala offers an Actors model - And the powerful Akka framework • Clojure is immutable by default - Has agents (like actors) & shared-nothing by default - Also has a Software Transactional Memory (STM) model • Groovy has GPARs
  • 57. Acknowledgments • All Otter images Creative Commons or Fair Use • Matt Raible - 20 criteria of selecting web frameworks • Ola Bini - Polyglot pyramid • Photos owned by Flickr Users - moff, spark, sodaro, lonecellotheory, tomsowerby - farnsworth, prince, marcus_jb1973, mliu92, Ed Zitron, - NaturalLight & monkeywing • Dancing Otter by the amazing Nicola Slater @ folksy
  • 58. Where is our beer!? 58
  • 59. Thanks for listening! (@kittylyst, @karianna)

Editor's Notes

  1. TODO: If we have time replace code images with actual code\n
  2. Reference the ignite talk\n
  3. \n
  4. \n
  5. * Hands up for Java 6, 5, 4....\n* Hands up for j.u.c\n
  6. * Hands up if you’re daunted by the refactoring that would be required\n\n
  7. \n
  8. \n
  9. \n
  10. They are Apex Predators - think of them in that way\n
  11. The practice of managing your threads (or Otters!) is governed by four forces (after Doug Lea)\n
  12. \n
  13. \n
  14. \n
  15. It’s a little-known fact that Otters are scared of log-linear graphs\n
  16. * TODO: Need better wording for Code is restricted\n* Very successful within own frame of reference, but caveats\n* Reference Mechanical sympathy again here\n
  17. * So you can contort more and more, but you’re chasing diminishing returns\n* Ultimately, that exponent gap between clock speed and memory will do for you\n
  18. * Raise your hand if you use the process monitor on Ubuntu or another Linux. OK, have you seen how Java processes behave under that?\n
  19. \n
  20. * Explain that we’re going to show replacements for using synchronized\n* Raise your hand if you know why we use the keyword “synchronized” to denote a critical section in Java\n
  21. \n
  22. How performant do we think FS objects are?\nImmutability is good, but watch the copy-cost\n
  23. \n\n
  24. happens-before defines a “partial order” (if you’re a mathematician)\n\n
  25. \n
  26. * Hands up if you know what a (Non-Uniform Memory Access) NUMA architecture is?\n* In some ways, this is actually easier to explain on a NUMA arch...\n
  27. \n
  28. \n
  29. * Lock can in some cases directly replace synchronized, but is more flexible\n* MONITORENTER & MONITOREXIT\n* We use reentrant lock else recursive code deadlocks real quick\n
  30. TODO: Are we going to do the condition wait/notify sketch?\n
  31. Condition takes the place of wait() / notify() (Object monitors)\nTalk to the cases - 1 putter, 1 taker, many putters, few takers, few putters, many takers - think about this stuff at the design stage\n
  32. \n
  33. “What’s the worst thing that can happen if you’re iterating over the keys of a regular HashMap and someone alters it underneath you?”\n
  34. Basically it’s a drop-in replacement for regular HashMap\n
  35. \n
  36. Not quite a drop-in replacement - Performance needs to be thought about\nTODO Need to fix code sample so we have iterators\n
  37. \n
  38. \n
  39. \n
  40. \n\n
  41. BlockingQueue offers several different ways to interact with it (see the Javadoc)\n\n
  42. \n
  43. offer is similar to add but doesn’t throw exceptions\nProducers adding trade orders for example\nTODO Theatresports?\n
  44. Final building block for modern concurrent applications with Java\n
  45. \n
  46. \n
  47. * Let’s step through a quasi-realistic example\n* That cancel() code begs for some of that lambda treatment huh!\n
  48. \n
  49. \n\n
  50. As well as RecursiveAction there’s the more general ForkJoinTask\n\n
  51. Multithreaded Quicksort - shows a speedup from O(nlog n) to O(n) - not quite linear, but not bad\n
  52. So this is pretty much a statement of the state-of-the-art in Java concurrency\n
  53. \n
  54. * Thread is the assembly language of concurrency\n* We need to move to a more managed model\n
  55. \n
  56. * Coroutines, etc\nLMAX’s OSS “Disruptor” framework proves this\nAkka, Clojure etc\n
  57. \n
  58. \n
  59. \n
  60. \n