O'Reilly logo
live online training icon Live Online training

Mastering Threads in Java

Topic: Software Development
Heinz Kabutz

Let's write a ThreadPool together. First exercise. A sea of scared faces. Is the teacher kidding?

"Mastering Threads in Java" is an intense two- day course that starts with the basics of threads. We then look at laws that help us to write correct threaded code. We then go to higher-level constructs that make threading much easier to use. We end with diagnosing race conditions and deadlocks.

Each section ends with exercises that you can solve to hone your skills as a Java programmer. Some of these exercises (write a ThreadPool) have challenged hundreds of Java programmers. In solving these, we learn how inter-thread communication works. We learn how to keep our classes thread-safe. We also learn about thread states.

We have a detailed walkthrough for each exercise. This helps in the learning aspect as they get feedback from the instructor.

We use OpenJDK 11 for this course, taking full advantage of the new var syntax. However, programmers with Java 6 or 7 knowledge will also be able to follow.

We are selective on what we cover in this. Our intention is to create a short course that will teach you enough to help you in your everyday work. Job interview questions on concurrency should not be hard after this. "Mastering Threads in Java" is an introductory course on the topic of concurrency..

What you'll learn-and how you can apply it

  • Basics of threads, how to synchronize correctly, shared memory between threads
  • “The Secrets of Concurrency” – ten laws to help programmers understand threading better
  • Applied threading techniques – using higher-level classes like BlockingQueue and Executors
  • Threading Problems – solving issues like data races and deadlocks

This training course is for you because...

  • Intermediate Java programmers wanting to learn how threading works in Java

Prerequisites

  • Should be very comfortable with at least Java 8 code. Java 11+ is an advantage.

Course Set-up

Please install the following before the class begins: - JDK 11 or later - IntelliJ 2020.2 or later - Git

Further set-up instructions will be provided at the start of the class.

Recommended Preparation

Recommended Follow-up:

  • Live Online Training: Dynamic Proxies in Java, by Heinz Kabutz: search the O'Reilly Learning Platform for an upcoming date
  • Live Online Training: Design Patterns in Java, by Heinz Kabutz: search the O'Reilly Learning Platform for an upcoming date
  • Live Online Training: Solving Java Memory Leaks, by Kirk Pepperdine: search the O’Reilly Learning Platform for an upcoming date
  • Sign up to The Java Specialists’ Newsletter for lots of tips on the latest Java developments

About your instructor

  • Heinz Kabutz is the author of “The Java Specialists’ Newsletter”, a publication enjoyed by tens of thousands of Java experts in over 145 countries. His book “Dynamic Proxies (in German)” was #1 Bestseller on Amazon.de in Fachbücher für Informatik for about five minutes until Amazon fixed their algorithm. Thanks to a supportive mother, he has now sold 5 copies.

    Heinz’s Java Specialists’ newsletter is filled with amusing anecdotes of life on the Island of Crete. He is a popular speaker at all the best Java conferences around the world, and also at some of the worst. He teaches Java courses in classrooms around the world, where his prime objective is to make absolutely sure that none of his students fall asleep. He is not always successful.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Day 1

Segment 1: Welcome (10 mins)

  • Mastering Threads
  • Hearty Welcome

Segment 2: Basics of Threads (1 hour, 45 mins with exercises)

  • Basics of Threads
  • Why Use Threads?
  • Threading Models
  • Parallel Computing vs Concurrent Computing
  • Java Memory Model (JSR 133)
  • Creating New Thread
  • Shared Memory with Multithreading
  • Synchronized
  • Thread States
  • Inter-thread communication
  • Exercise/activity description: First exercise is to write a thread pool, learning how inter-thread communication is done

Segment 3: The Secrets of Concurrency (1 hour, 45 mins with exercises)

  • Sabotaged Doorbell
  • Distracted Spearfisherman
  • Overstocked Haberdashery
  • Blind Spot
  • Leaked Memo
  • Corrupt Politician
  • Micromanager
  • Cretan Driving
  • Sudden Riches
  • Uneaten Lutefisk
  • Exercise/activity description: We improve the inter-thread communication by fixing the handling of the InterruptedException

Day 2

Segment 4: Applied Threading Techniques (1 hour, 45 mins with exercises)

  • Executors
  • Callable and Future
  • CompletionService
  • CountDownLatch
  • Semaphore
  • BlockingQueues
  • Unhandled Exceptions
  • ThreadLocal
  • Stopping Threads
  • Shutdown Hooks
  • Exercise/activity description: Our thread pool is refactored to use blocking queues for inter-thread communication

?Segment 5: Threading Problems (1 hour, 45 mins with exercises)

  • Race Condition
  • Starvation
  • Fairness
  • Deadlock
  • Analyzing Thread Dumps
  • Livelock
  • Exercise/activity description: We need to detect and fix race conditions and deadlocks in these exercises

Segment 5: Conclusion (10 mins)

Course wrap-up and next steps