Java SE 8 Programmer II study materials
Java SE 8 Programmer II study resources

Java SE 8 Programmer II: Concurrency

Java SE 8 Programmer II · Work through your exam, one topic at a time.

All guide sections

Concurrency

Concurrency tests thread creation, synchronization, visibility, executors, and concurrent collections.

Core rules

  • Create a thread by extending Thread and overriding run(), or by passing a Runnable to Thread. Call start(), not run(), to create a new thread.
  • Thread states include NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED.
  • synchronized provides mutual exclusion and visibility on the same lock object; wait(), notify(), and notifyAll() must be called while holding that object's monitor.
  • volatile guarantees visibility but not atomicity; AtomicInteger and related classes provide atomic compound operations.
  • ExecutorService decouples task submission from thread management. submit(Callable<T>) returns Future<T>; get() blocks and throws ExecutionException wrapping the task's exception.
  • Concurrent collections (ConcurrentHashMap, CopyOnWriteArrayList, BlockingQueue) are preferred over synchronized wrappers for concurrent access.
  • shutdown() stops accepting tasks and lets queued tasks finish; shutdownNow() interrupts and returns queued tasks.

Worked example

ExecutorService ex = Executors.newSingleThreadExecutor(); Future<Integer> f = ex.submit(() -> 2 + 3); int v = f.get(); ex.shutdown(); submit runs the Callable asynchronously; get() waits for completion and returns 5. If the callable threw, get() would throw ExecutionException whose cause is the original exception.

Traps

  • Calling run() directly executes on the current thread.
  • Synchronizing on different objects does not protect shared state.
  • Thread.sleep() does not release locks; wait() does.
  • A Future.get() without a timeout can block forever.
  • Modifying a collection while iterating can throw ConcurrentModificationException; use the iterator's remove() or a concurrent collection.
  • shutdownNow() does not guarantee immediate termination.