The document provides an overview of modern Java concurrency. It discusses how concurrency has become important for performance as CPUs evolved to include multiple cores. It summarizes the java.util.concurrent utilities and common concurrency constructs like locks, queues, thread pools. It advocates using higher-level concurrency abstractions and more immutable and thread-safe collections to make concurrent programming easier.
* Hands up for Java 6, 5, 4.... * Hands up for j.u.c
Hands up if you’re daunted by the refactoring that would be required
The practice of managing your threads (or Otters!) is governed by four forces (after Doug Lea)
It’s a little-known fact that Otters are scared of log-linear graphs
Very successful within own frame of reference, but caveats Reference Mechanical sympathy again here
* So you can contort more and more, but you’re chasing diminishing returns * Ultimately, that exponent gap between clock speed and memory will do for you
* Raise your hand if you use the process monitor on Ubuntu or another Linux. OK, have you seen how Java processes behave under that?
* Explain that we’re going to show replacements for using synchronized * Raise your hand if you know why we use the keyword “synchronized” to denote a critical section in Java
How performant do we think FS objects are? Immutability is good, but watch the copy-cost
happens-before defines a “partial order” (if you’re a mathematician) JMM is even worse than the generics constraints part of the spec!
* Hands up if you know what a NUMA architecture is? * In some ways, this is actually easier to explain on a NUMA arch...
Lock basically replaces synchronized, but is more flexible MONITORENTER & MONITOREXIT
Condition takes the place of wait() / notify() (Object monitors) Talk to the cases - 1 putter, 1 taker, many putters, few takers, few putters, many takers - think about this stuff at the design stage
Basically it’s a drop-in replacement for regular HashMap “ 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?”
Not quite a drop-in replacement - Performance needs to be thought about Talk about Iterators
BlockingQueue offers several different ways to interact with it (see the Javadoc)
offer is similar to add but doesn’t throw exceptions Producers adding trade orders for example
Let’s step through a quasi-realistic example
As well as RecursiveAction there’s the more general ForkJoinTask
Multithreaded Quicksort - shows a speedup from O(nlog n) to O(n) - not quite linear, but not bad
So this is pretty much a statement of the state-of-the-art in Java concurrency
* Thread is the assembly language of concurrency * We need to move to a more managed model