15) Concurrency: Threads, Executors, Futures
Goal
Understand how to run tasks concurrently and how to avoid common mistakes.
Thread basics
var t = new Thread(() -> System.out.println("running"));
t.start();
Prefer executors
ExecutorService pool = Executors.newFixedThreadPool(4);
Future<Integer> result = pool.submit(() -> 40 + 2);
System.out.println(result.get());
pool.shutdown();
Common hazards
- data races (shared mutable state)
- deadlocks
- forgetting to shutdown executors
Exercises
- Run 10 tasks in a fixed thread pool and collect results.
- Implement a thread-safe counter using
AtomicInteger.
- Demonstrate a race condition, then fix it.
Table of contents
- Getting Started: Install, run, and your first program
- Java Basics: types, variables, operators, formatting
- Control Flow: if/switch/loops
- Methods: parameters, return values, overloading
- OOP: classes, objects, encapsulation
- Inheritance & Polymorphism (and when not to use them)
- Interfaces, abstract classes, and design basics
- Exceptions and error handling
- Strings, files, and I/O basics
- Collections: List/Set/Map and Big-O intuition
- Generics (the useful parts)
- Lambdas & Streams
- Dates and time (java.time)
- Testing with JUnit 5 (basics)
- Concurrency: threads, executors, futures
- JVM basics: memory, GC, performance habits
- Build tools: Maven essentials (recommended)
- Next steps: projects to build