Fatskills
Practice. Master. Repeat.
Study Guide: Java: Multithreading - Thread Creation, Extending Thread, Implementing Runnable
Source: https://www.fatskills.com/surgery/chapter/java-multithreading-thread-creation-extending-thread-implementing-runnable

Java: Multithreading - Thread Creation, Extending Thread, Implementing Runnable

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~5 min read

What This Is and Why It Matters

Thread creation in Java is fundamental for building responsive and efficient applications. This topic covers extending the Thread class and implementing the Runnable interface, two primary methods for creating threads. Mastering this is crucial for concurrent programming, which is essential for tasks like handling multiple user requests, performing background operations, and optimizing resource usage. In exams like the Oracle Certified Professional: Java SE 8 Programmer, this topic carries significant weight. Misunderstanding it can lead to inefficient code, deadlocks, or race conditions, severely impacting application performance and reliability.

Core Knowledge (What You Must Internalize)

  • Thread: A lightweight process that can be managed independently by a scheduler. (Why this matters: It allows concurrent execution of tasks.)
  • Runnable: An interface that should be implemented by any class whose instances are intended to be executed by a thread. (Why this matters: It provides a common protocol for objects that wish to execute in parallel.)
  • start(): Method to begin execution of a new thread. (Why this matters: It initiates the thread's lifecycle.)
  • run(): Method containing the code that constitutes the new thread. (Why this matters: It defines the thread's task.)
  • Difference between Thread and Runnable: Thread is a class, while Runnable is an interface. (Why this matters: Understanding this distinction helps in choosing the right approach for thread creation.)
  • Daemon Threads: Background threads that do not prevent the JVM from exiting. (Why this matters: Useful for tasks that should not block application termination.)

Step?by?Step Deep Dive

  1. Extend the Thread Class
  2. Action: Create a new class that extends Thread.
  3. Principle: Inheritance allows the new class to override the run() method.
  4. Example: java public class MyThread extends Thread { public void run() { System.out.println("Thread is running"); } }
  5. Pitfall: Overriding run() instead of start(). start() begins the thread's execution.

  6. Implement the Runnable Interface

  7. Action: Create a new class that implements Runnable.
  8. Principle: Implementing Runnable allows the class to be used by any thread.
  9. Example: java public class MyRunnable implements Runnable { public void run() { System.out.println("Runnable is running"); } }
  10. Usage: java Thread thread = new Thread(new MyRunnable()); thread.start();

  11. Starting a Thread

  12. Action: Call the start() method on the thread object.
  13. Principle: start() schedules the thread for execution.
  14. Example: java MyThread thread = new MyThread(); thread.start();
  15. Pitfall: Directly calling run() will execute it in the current thread, not a new one.

  16. Daemon Threads

  17. Action: Set a thread as daemon using setDaemon(true).
  18. Principle: Daemon threads run in the background and do not prevent JVM exit.
  19. Example: java Thread daemonThread = new Thread(new MyRunnable()); daemonThread.setDaemon(true); daemonThread.start();

How Experts Think About This Topic

Experts view thread creation as a strategic decision based on the task's nature. They prefer implementing Runnable for flexibility and separation of concerns, reserving extending Thread for cases where thread-specific behavior is needed. They also consider thread lifecycle management and resource optimization to avoid common concurrency issues.

Common Mistakes (Even Smart People Make)

  1. The mistake: Directly calling run() instead of start().
  2. Why it's wrong: It executes the code in the current thread, not a new one.
  3. How to avoid: Always use start() to initiate a new thread.
  4. Exam trap: Questions that trick you into calling run() directly.

  5. The mistake: Overriding start() instead of run().

  6. Why it's wrong: start() is meant to begin the thread's lifecycle.
  7. How to avoid: Remember that run() contains the thread's task.
  8. Exam trap: Code snippets with incorrect method overrides.

  9. The mistake: Not handling thread interruption properly.

  10. Why it's wrong: Can lead to unresponsive threads and resource leaks.
  11. How to avoid: Check for interruption using Thread.interrupted().
  12. Exam trap: Scenarios requiring proper interruption handling.

  13. The mistake: Ignoring daemon threads.

  14. Why it's wrong: Can cause background tasks to block JVM exit.
  15. How to avoid: Use setDaemon(true) for background tasks.
  16. Exam trap: Questions about application termination behavior.

Practice with Real Scenarios

  1. Scenario: A web server needs to handle multiple client requests concurrently.
  2. Question: How would you implement this using threads?
  3. Solution: Implement Runnable for handling client requests. java public class ClientHandler implements Runnable { public void run() { // Handle client request } }
  4. Answer: Use Runnable for flexibility.
  5. Why it works: Separates request handling logic from thread management.

  6. Scenario: A background task should not prevent the application from closing.

  7. Question: How would you implement this task?
  8. Solution: Set the thread as daemon. java Thread backgroundTask = new Thread(new RunnableTask()); backgroundTask.setDaemon(true); backgroundTask.start();
  9. Answer: Use setDaemon(true).
  10. Why it works: Daemon threads do not block JVM exit.

  11. Scenario: A thread needs to perform a specific action when started.

  12. Question: How would you implement this?
  13. Solution: Extend Thread and override run(). java public class SpecificThread extends Thread { public void run() { // Specific action } }
  14. Answer: Extend Thread for specific behavior.
  15. Why it works: Allows customization of thread behavior.

Quick Reference Card

  • Core rule: Use Runnable for flexibility, Thread for specific behavior.
  • Key formula: thread.start() to begin execution.
  • Critical facts:
  • Runnable is an interface.
  • Thread is a class.
  • Use setDaemon(true) for background tasks.
  • Dangerous pitfall: Calling run() directly instead of start().
  • Mnemonic: "Start to run, not run to start."

If You're Stuck (Exam or Real Life)

  • Check: Thread lifecycle methods (start(), run()).
  • Reason: From first principles of concurrency and task separation.
  • Estimate: Thread behavior based on daemon status.
  • Find the answer: Refer to Java documentation or trusted resources.

Related Topics

  • Thread Lifecycle: Understand the states a thread goes through.
  • Synchronization: Learn how to manage access to shared resources.
  • Thread Pools: Explore efficient thread management using executors.