Skip to content

Instantly share code, notes, and snippets.

@christianromney
Created December 13, 2019 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianromney/a234c0f5b1074c5920edda8c8539692d to your computer and use it in GitHub Desktop.
Save christianromney/a234c0f5b1074c5920edda8c8539692d to your computer and use it in GitHub Desktop.

Concurrency on the JVM

Thread - this class represents a thread of execution. Unit of scheduling on the CPU.

ThreadLocal - a per-thread variable not visible to other threads

ThreadGroup - a grouping that allows convenient management for set of threads

hierarchical tree

all threads can see their group, but not necessarily their group’s parent group

ForkJoinTask - a thread-like entity that is much lighter weight than a normal thread

Runnable - an interface that classes wanting to execute in a thread should implement: void run()

Future - the result of an asynchronous computation

Huge numbers of tasks and subtasks may be hosted by a small number of actual threads

lightweight form of Future

intended use as computational tasks calculating pure functions

should avoid blocking

should also not perform blocking IO

FutureTask is implementation of Future interface

Executor - interface for thread-like subsystems void execute(final Runnable)

Executes the given command at some time in the future

Task may execute in:

new thread

existing worker thread

calling thread

Task may run:

sequentially

concurrently

Thread management interface. Functionality depends on concrete class:

ThreadPoolExecutor - fixed thread pool

May improve performance when there are several async tasks due to decreased startup
Provides a way to bound resource consumption
threads kept alive for keep alive time after completion; can reduce resource consumption when idle
beforeExecute / afterExecute hooks

t***** new threads created by a ThreadFactory

defaultThreadFactory makes
all threads in same ThreadGroupall threads Normal priorityall threads non-daemon
privilegedThreadFactory
same settings as default, buthave same privileges as current thread

ScheduledThreadPoolExecutor - same, but can execute after certain time or at intervals

a little bit like Javascript’s setTimeout and setInterval
delayed tasks guaranteed not to execute sooner than delay (but exact, real-time not guaranteed)

ForkJoinPool - work-stealing scheduler good for certain types of CPU-bound parallel processing

work-stealing: all threads attempt to find and execute subtasks created by other active tasks

Executors factory

Executors/newCachedThreadPool

unbounded pool size
automatic reclamation

Executors/newFixedThreadPool(int)

fixed size thread pool

Executors/newSingleThreadExecutor

single background thread

Queues

ConcurrentLinkedQueue - non-blocking, thread-safe

BlockingQueue - blocking put / take

LinkedBlockingQueue

unbounded queue when no capacity set
based on linked nodes, orders elements FIFO (first-in-first-out)

ArrayBlockingQueue

bounded queue
backed by an array, orders FIFO

SynchronousQueue

each insert operation must wait for a corresponding remove operation by another thread, and vice versa

PriorityBlockingQueue

uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations

DelayQueue

an element can only be taken when its delay has expired

Timeouts (TimeUnit)

most classes in java.util.concurrent provide method versions that timeout

timeouts are “best effort” (approx. time) not perfectly accurate due to thread scheduling

Don’t wait at all 0 or negative timeout

Wait forever Long.MAX_VALUE

Synchronization Primitives

Semaphore - red-light/green-light

CountDownLatch - block until multiple conditions (signals, events, etc) hold true

CyclicBarrier - resettable, multiway synchronization point

useful when a fixed sized party of threads must occasionally wait for each other

barrier is cyclic because it can be re-used after the waiting threads are released

Exchanger - allows 2 threads to exchange objects at rendezvous points

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment