Creating Threads in Java: Extending Thread or Implementing Runnable

Day 65 of Sharing What I’ve Learned 🚀 Creating Threads in Java — Turning Concepts into Execution After building a clear understanding of programs, processes, and threads, the next step was to move from theory to practice — how threads are actually created and used in Java. Understanding what a thread is is important. But knowing how to create and control it is where things start getting real. 🔹 Two Ways to Create Threads in Java Java provides two primary ways to create threads: 1️⃣ Extending the Thread class Here, we create a class that extends Thread and override the run() method. class MyThread extends Thread { public void run() { System.out.println("Thread is running..."); } } public class Main { public static void main(String[] args) { MyThread t1 = new MyThread(); t1.start(); // starts a new thread } } 👉 start() is important — it creates a new thread and calls run() internally. 2️⃣ Implementing the Runnable interface This is the more flexible and commonly used approach. class MyRunnable implements Runnable { public void run() { System.out.println("Thread using Runnable..."); } } public class Main { public static void main(String[] args) { Thread t1 = new Thread(new MyRunnable()); t1.start(); } } 👉 This approach is preferred because Java doesn’t support multiple inheritance, but it allows implementing multiple interfaces. 🔹 Key Difference Thread → Direct but less flexible Runnable → More scalable and widely used in real-world applications 🔹 Important Observation Creating a thread is not just about writing run() 👉 It’s about understanding how execution flow changes run() → normal method call (no new thread) start() → creates a new thread (parallel execution) 🔹 Simple Perspective Thread creation = defining a task start() = actually running it concurrently 🔹 Why This Matters This is where applications start becoming powerful: 👉 Multiple tasks can run independently 👉 Better performance and responsiveness 👉 Foundation for advanced concepts like thread synchronization and concurrency 🔹 My Realization Earlier, execution felt linear — one step after another. Now it’s clear that: 👉 Execution doesn’t have to wait 👉 Tasks can run side by side 👉 Efficiency comes from how we structure execution This is just the next layer — Up next: controlling threads and managing their lifecycle. #Java #Multithreading #Programming #JavaDeveloper #100DaysOfCode #DeveloperJourney #Day65 Grateful for guidance from TAP Academy Sharath R kshitij kenganavar

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories