What is a thread in Java and how can we create it?

A thread is the smallest unit of execution within a Java program. It allows multiple tasks to run concurrently, enabling better utilization of system resources and improved application responsiveness. Java provides built-in support for multithreading, making it easier to perform multiple operations simultaneously.

Key Points: • A thread represents an independent path of execution. • Multiple threads can run concurrently within a single process. • Threads help improve application performance and responsiveness. • Java supports thread creation through the Thread class and Runnable interface. • Multithreading is commonly used in web servers, games, and background processing tasks.

Why Do We Need Threads?

Without threads:

• Tasks execute sequentially. • Long-running operations can block the application.

With threads:

• Multiple tasks can execute concurrently. • Applications remain responsive while performing background work.

Example:

A banking application can:

• Process transactions • Send notifications • Generate reports

at the same time using multiple threads.

Method 1: Extending the Thread Class

A class can create a thread by extending the Thread class and overriding the run() method.

Code Example:

class MyThread extends Thread {

    @Override
    public void run() {

        System.out.println(
                "Thread is running");
    }
}

public class Demo {

    public static void main(String[] args) {

        MyThread thread =
                new MyThread();

        thread.start();
    }
}

Output:

Thread is running

Method 2: Implementing the Runnable Interface

A class can implement Runnable and provide the task inside the run() method.

Code Example:

class MyTask implements Runnable {

    @Override
    public void run() {

        System.out.println(
                "Task executed by thread");
    }
}

public class Demo {

    public static void main(String[] args) {

        Thread thread =
                new Thread(
                        new MyTask());

        thread.start();
    }
}

Output:

Task executed by thread

Why Runnable Is Preferred?

Implementing Runnable is generally preferred because:

• Java supports single inheritance. • The class can extend another class if needed. • Better separation between task and thread management.

Thread Lifecycle

A thread typically moves through the following states:

1. New

Thread object is created.

2. Runnable

Thread is ready to run.

3. Running

Thread is actively executing.

4. Blocked / Waiting

Thread waits for a resource or event.

5. Terminated

Thread completes execution.

Real-World Example:

Consider a food delivery application.

Thread 1:

• Accept order

Thread 2:

• Process payment

Thread 3:

• Send notifications

All tasks can run concurrently without waiting for each other.

Benefits of Multithreading

• Better CPU utilization • Faster execution of independent tasks • Improved responsiveness • Efficient background processing • Enhanced user experience

Common Uses of Threads

• Web servers • File downloads • Email sending • Real-time applications • Background jobs • Gaming applications

Interview Tip: A concise interview answer is:

"A thread is an independent path of execution within a Java program that enables concurrent task processing. Threads can be created either by extending the Thread class or by implementing the Runnable interface. Among the two approaches, implementing Runnable is generally preferred because it provides greater flexibility and supports better object-oriented design."