Busy Java Developer's Guide to Concurrency

ted.neward@newardassociates.com | Blog: http://blogs.newardassociates.com | Github: tedneward | LinkedIn: tedneward

Objectives

Java has a built-in multithreading model

Concepts of Concurrency

Multitasking, multithreading, oh my!

Concepts

Why go concurrent?

Concepts

Concurrency carries with it some costs

Concepts

So why do it?

JVM Threading

Walk and chew gum...

JVM Threading

`java.lang.Thread`: Java thread object

JVM Threading

Creating new thread

JVM Threading

Starting the thread

JVM Threading


Thread t = new Thread( () => System.out.println("Howdy, threaded world!") );
Thread t2 = new Thread(new Runnable() {
  public void run() {
    System.out.println("Howdy, old-school Thread!");
  }
};
Thread t3 = new Thread()
t.start();
t2.start();

JVM Threading

Foreground vs. background

JVM Threading

Thread priority determines scheduling

JVM Threading

Thread lifecycle

JVM Threading

From the outside of the Thread...

JVM Threading

Threads complete in one of several ways

JVM Executors

JVM Executors

Executor

JVM Executors

ExecutorService

JVM Executors

Executors

JVM 'Virtual' Threads

Threads but not really

Synchronization

Keeping the cookie jar free of too many hands at once

Synchronization

A concurrent joke: Why did the multithreaded chicken cross the road?

Synchronization

A concurrent joke: Why did the multithreaded chicken cross the road?

Synchronization

A concurrent joke: Why did the multithreaded chicken cross the road?

Synchronization

A concurrent joke: Why did the multithreaded chicken cross the road?

Synchronization

A concurrent joke: Why did the multithreaded chicken cross the road?

... this joke never ends!

Synchronization

Concurrent activities usually require some degree of synchronization

Synchronization

Multiple paths of execution create multiple interleaving permutations of execution

Synchronization

Concurrent synchronization is a balancing act

Patterns

Patterns for safely representing/managing state:

JVM Atomic Operations

Updates across threads

JVM Atomic Operations

A sample concurrency problem:

JVM Atomic Operations

The JVM memory model

JVM Atomic Operations

"Atomic types" allow for atomic update w/o worry

JVM Exclusionary Regions

One at a time, please....

Synchronization

A sample concurrency problem

Mutual exclusion

Monitors are the basis for JVM thread synch

Mutual exclusion

Monitor ownership

java.util.concurrent.lock

Locks allow developers to constrain execution

JVM Thread Coordination

First you, now you, not you yet...

Synchronization

A sample concurrency problem

Signalling

Threads sometimes need to signal each other

Conditional locking

Conditional locking is very common

Collections

Java5 introduced some Collections classes w/additional concurrency-safe methods

JVM Exclusionary Regions

One at a time, please....

Synchronization

A sample concurrency problem

Mutual exclusion

Monitors are the basis for JVM thread synch

Mutual exclusion

Monitor ownership

java.util.concurrent.lock

Locks allow developers to constrain execution

JVM Atomic Operations

Updates across threads

JVM Atomic Operations

A sample concurrency problem:

JVM Atomic Operations

The JVM memory model

JVM Atomic Operations

"Atomic types" allow for atomic update w/o worry

JVM Thread Coordination

First you, now you, not you yet...

Synchronization

A sample concurrency problem

Signalling

Threads sometimes need to signal each other

Conditional locking

Conditional locking is very common

Collections

Java5 introduced some Collections classes w/additional concurrency-safe methods

Summary

Summary

Credentials

Who is this guy?