How to Start a Thread in Java?

Threads are sometimes called lightweight processes.

Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process.

Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files.

Every application has at least one thread — or several if you count "system" threads that do things like memory management and signal handling.

But from the application programmer's point of view, you start with just one thread, called the main thread. This thread has the ability to create additional threads.

In this article we will see How to Start a Thread in Java with examples.

There are two ways to create/start thread in java.

1). By extends the java.lang.Thread class
2). By implementing the Runnable Interface

We will see them one by one.

1). By extends the java.lang.Thread class

The Thread class itself implements Runnable, though its run method does nothing. An application can subclass Thread, providing its own implementation of the run method.

The Thread class defines a number of methods useful for thread management.

These include static methods, which provide information about or affect the status of, the thread invoking the method. The other methods are invoked from other threads involved in managing the thread and Thread object.

package com.javacodestuffs.core.multithreading; class MyThread extends Thread { public void run() { System.out.println(Thread.currentThread().getId() + " Thread is Running "); } } public class MyThreadTest { public static void main(String[] args) { for (int i = 0; i < 10; i++) { MyThread myThread = new MyThread(); myThread.start(); } } } output: 22 Thread is Running 24 Thread is Running 23 Thread is Running 25 Thread is Running 21 Thread is Running 27 Thread is Running 26 Thread is Running 28 Thread is Running 30 Thread is Running 29 Thread is Running '

2). By implementing the Runnable Interface

The Runnable interface defines a single method, run, meant to contain the code executed in the thread.

package com.javacodestuffs.core.multithreading; public class MyRunnableThread implements Runnable { public void run() { System.out.println("Hello from a thread " + Thread.currentThread().getId() + " !!!"); } public static void main(String args[]) { for (int i = 0; i < 10; i++) { Thread thread = new Thread(new MyRunnableThread()); thread.start(); } } } output: Hello from a thread 21 !!! Hello from a thread 24 !!! Hello from a thread 23 !!! Hello from a thread 22 !!! Hello from a thread 26 !!! Hello from a thread 25 !!! Hello from a thread 27 !!! Hello from a thread 28 !!! Hello from a thread 29 !!! Hello from a thread 30 !!!

Which is better?

1). If we extend the Thread class, then we cannot extend other classes. But, if we implement the Runnable interface, our class can still extend other base classes.

2). If you need to use yield(), interrupt() methods in your applications, then you should use Thread class to start a new thread.

If we are not overriding run() method

If we are not overriding run() method then Thread class run() method will be executed which has empty implementation and hence we won't get any output.

package com.javacodestuffs.core.multithreading; class MyThread extends Thread {} public class MyThreadTest1 { public static void main(String[] args) { MyThread t = new MyThread(); t.start(); } }

Overloading of run() method

We can overload run() method but Thread class start() method always invokes no argument run() method the other overload run() methods we have to call explicitly then only it will be executed just like a normal method.

package com.javacodestuffs.core.multithreading; class MyThread extends Thread { public void run() { System.out.println("no arg method"); } public void run(int i) { System.out.println("int arg method"); } } public class ThreadDemo { public static void main(String[] args) { MyThread t = new MyThread(); t.start(); } } output: no arg method

Overriding the start() method 

If we override start() method then our start() method will be executed just like a normal method call and no new Thread will be started.

package com.javacodestuffs.core.multithreading; class MyThread extends Thread { public void start() { System.out.println("start method"); } public void run() { System.out.println("run method"); } } public class ThreadDemo1 { public static void main(String[] args) { MyThread t = new MyThread(); t.start(); System.out.println("main method"); } } output: start method main method

To know more about Multi-Threading in Java, please read this article.

Post/Questions related to How to Start a Thread in Java

How to create multiple threads in java?
How to create multiple threads in java?
How to check the currently running thread in java?
How to decide number of threads in java?
How to find long-running threads in java?
How to create a custom thread in java?
How to pass data from one thread to another thread in java?
Make a java object which is accessible to all threads by reference of address
and store your data in that object so that all threads can access it.
How to run two threads simultaneously in java?
How to run a thread for specific time in java?
How to check if a thread is completed in java?
What is a thread in java with example?

In this article, we have seen  How to Start a Thread in Java with examples. All source code in the article can be found in the GitHub repository.