Busy Java Developer's Guide to Concurrency

ted@tedneward.com | Blog: http://blogs.tedneward.com | Twitter: tedneward | 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?

Java Threading

Walking and chewing gum at the same time

java.lang.Thread

java.lang.Thread: Java thread object

java.lang.Thread

Creating new thread

java.lang.Thread

Starting the thread

java.lang.Thread


Thread t = new Thread(new Runnable() {
  public void run() {
    System.out.println("Howdy, new Thread!");
  }
};
Thread t2 = new Thread( () => System.out.println("Or lambda!") );
t.start();
t2.start();

java.lang.Thread

Foreground vs. background

java.lang.Thread

Thread priority determines scheduling

java.lang.Thread

Thread lifecycle

java.lang.Thread

From the outside of the Thread...

java.lang.Thread

Threads complete in one of several ways

java.lang.Thread

Exceptions & Threads

java.lang.ThreadGroup

ThreadGroup was intended as administrative construct to manage groups of Threads

java.util.concurrent.Callable

Callable provides result & exception API

java.util.concurrent.Future

Future tracks pending results

Two subtypes of interest

java.util.concurrent.Executor

Executor

java.util.concurrent.ExecutorService

ExecutorService

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:

Synchronization

Preventing Thread collisions

Synchronization

A sample concurrency problem:

Memory Models

The JVM memory model

java.util.concurrent.atomic

Java5 introduced some "atomic variables" that allow for atomic update w/o worry

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

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?