1Z0-771 study materials
1Z0-771 study resources

1Z0-771: Concurrency and Multithreading

1Z0-771 · Work through your exam, one topic at a time.

All guide sections

Concurrency and Multithreading

Domain 6 tests thread lifecycle, safe publication, and the java.util.concurrent toolkit: how to start work, how to stop it cleanly, and which classes remove the need for hand-written synchronization.

Threads

  • Create with Thread or Runnable; start() launches a new thread, while run() merely executes on the current thread.
  • States: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED. join() waits for termination; interrupt() sets the interrupt flag and wakes blocking calls with InterruptedException.
  • Callable<V> returns a value and may throw checked exceptions; Runnable cannot.

Memory and safety

  • synchronized gives mutual exclusion plus visibility; volatile gives visibility and ordering but not atomicity, so count++ remains unsafe.
  • AtomicInteger, LongAdder and java.util.concurrent.locks cover common cases without synchronized blocks.
  • Immutable objects are inherently thread-safe; safely publish any shared mutable state.

Executors and concurrent collections

  • Executors.newFixedThreadPool, newSingleThreadExecutor, newCachedThreadPool; always shut down with shutdown() (graceful) or shutdownNow() (interrupts running tasks).
  • Future.get() blocks and throws ExecutionException wrapping the task's own exception; CompletableFuture composes asynchronous stages.
  • ConcurrentHashMap supports concurrent reads and updates and rejects null; CopyOnWriteArrayList suits read-heavy lists.

Worked example

ExecutorService pool = Executors.newFixedThreadPool(2);
Future<Integer> f = pool.submit(() -> 1 / 0);
try { f.get(); }
catch (ExecutionException e) { System.out.println(e.getCause()); } // ArithmeticException
finally { pool.shutdown(); }

The division error is captured inside the Future; get() rethrows it wrapped in ExecutionException.

Traps

  • Deadlock arises when two threads acquire the same locks in opposite orders; consistent lock ordering prevents it.
  • wait, notify and notifyAll require holding the monitor; await/signal belong to Condition.
  • A terminated thread cannot be restarted.