Concept
Running work on several threads, the races that follow, and the tools that prevent them.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Threads and thread safety doing its job.
var counter = new AtomicInteger();
var unsafe = new int[1];
try (var pool = Executors.newFixedThreadPool(8)) {
for (int i = 0; i < 10_000; i++) {
pool.submit(() -> { counter.incrementAndGet(); unsafe[0]++; });
}
}
System.out.println(counter.get()); // 10000
System.out.println(unsafe[0]); // usually less: lost updatesTreating count++ as one operation. It is a read, an add and a write, and another thread can slip in between them. Use AtomicInteger or synchronise the update.
Your roadmap puts these before Threads and thread safety, unless you already know them.
Once a session shows you understand Threads and thread safety, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.
Topics that build on Threads and thread safety.