Skip to content

Instantly share code, notes, and snippets.

@ajmurmann
Created September 19, 2017 04:19
Show Gist options
  • Save ajmurmann/09ce5d684e672300862a10abeed82624 to your computer and use it in GitHub Desktop.
Save ajmurmann/09ce5d684e672300862a10abeed82624 to your computer and use it in GitHub Desktop.
java_concurrency_chapter5.md

Chapter 5

Synchronized Collections

  • Process everything serially -> bad scalability
  • Iterating not safe, since not atomic -> need to block during iteration or copy
  • Compounded by hidden iterators like toString

Concurrent Collections

  • Allow concurrent access -> much faster

ConcurrentHashMap

  • Weakly consistent iterators that won't throw ConcurrentModificationException
    • Weaker guarantees by getSize and isEmpty
  • Provide more high level, atomic operations such as putIfAbsent

CopyOnWriteArrayList

  • Cost of copy might comes at cost. Reasonable if iterating far more than writing

BlockingQueue

  • Allows for easy implementation of producer/consumer pattern
  • Consumer can keep reading and will wait automatically
  • Write can keep writing
  • offer fails if it cannot write (nice for writing to disk etc.)
  • Great for throttling operations that otherwise would overwhelm the system
  • Various implementations to allow for different orders in the queue (PriorityBlockingQueue, ArrayBlockingQueue, etc.)
  • SynchronousBlockingQueue
    • threads wait for work, rather than work for threads
  • Makes for safe object hand-off (serial thread confinement)

Example: desktop search - Finding files and then indexing them

Deque ("deck")

  • Queue with two ends "head" and "tail"
  • Consumer can take from tail of other's deque if empty
  • Good for cases where consumer is also producer

Interrupts

Does InterruptedException indicate that the task should be aborted?

Synchronizers

Have some state and make threads wait till some conditions around that state

Latches

  • Starts closed and flips to open. Once opened cannot close.
  • CountdownLatch - Blocks till count hits 0
    • See Testing example: We wait for all threads to be ready (CountdownLatch(n)) then run work in parallel and wait with another latch for completion

FutureTask

  • start method kicks of processing
  • get blocks till done and then returns answer
  • Exceptions get returned as Throwable -> painful to unpack
  • Why do we throw a IllegalStateException if it's not a RuntimeException?

Semaphores

  • As long as counter is > 0 workers can proceed

Barrier

  • Like Latch but
    • Can be reused
    • all threads most come together at same time
  • Great for simulations (see game of life)

memoization example

  1. Synchronize reads -> slow because everything blocks. might be slower than without memoization
  2. ConcurrentHashMap -> scales better, but computation can happen in parallel
  3. Put Future in HashMap -> Unfortunate timing can still result in two calculations at same time
  4. Use putIfAbsent to put Future -> eliminates chance of double-computation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment