Hackforge Academy

Category: java

Creating a Thread by Extending Thread

Published on 27 Oct 2026

Explanation

You can extend Thread and override run(). Call start() to execute run() on a new thread. Calling run() directly is only a normal method call and does not start concurrent execution.

Code:

class GreetingThread extends Thread {
    @Override public void run() { System.out.println("Hello from thread"); }
}

public class ThreadSubclassDemo {
    public static void main(String[] args) { new GreetingThread().start(); }
}

Explanation

Runnable represents work that does not return a result. It separates the task from the execution mechanism and allows the class to extend another class if needed.

Code:

public class RunnableDemo {
    public static void main(String[] args) {
        Runnable task = () -> System.out.println("Runnable executed");
        new Thread(task).start();
    }
}

Explanation

Callable<T> represents a task that returns a value and may throw a checked exception. Submit it to an ExecutorService to receive a Future<T> for retrieving its result.

Code:

import java.util.concurrent.*;

public class CallableDemo {
    public static void main(String[] args) throws Exception {
        ExecutorService pool = Executors.newSingleThreadExecutor();
        try {
            Callable<Integer> task = () -> 20 + 22;
            System.out.println(pool.submit(task).get());
        } finally { pool.shutdown(); }
    }
}

Explanation

Thread is the execution mechanism, Runnable defines a task without a result, and Callable defines a task with a result. For most application code, use Runnable or Callable with an executor instead of subclassing Thread.

Code:

import java.util.concurrent.*;

public class TaskTypesDemo {
    public static void main(String[] args) throws Exception {
        ExecutorService pool = Executors.newSingleThreadExecutor();
        try {
            pool.submit(() -> System.out.println("Runnable task"));
            Future<String> result = pool.submit(() -> "Callable result");
            System.out.println(result.get());
        } finally { pool.shutdown(); }
    }
}

Explanation

The same Runnable instance can be passed to multiple threads. If the task captures shared mutable data, make sure concurrent access is safe; sharing a task does not automatically make its data thread-safe.

Code:

public class ReusableRunnableDemo {
    public static void main(String[] args) {
        Runnable task = () -> System.out.println(Thread.currentThread().getName());
        new Thread(task, "Worker-1").start();
        new Thread(task, "Worker-2").start();
    }
}

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community