Table of contents
1.
Introduction
2.
What is a Thread in Java?
3.
Thread class in Java
3.1.
Methods used in Thread class
4.
Runnable Interface in Java
5.
Advantages of Creating Threads
6.
Examples of Creating a Thread in Java
6.1.
Example 1
6.2.
Java
6.3.
Example 2
6.4.
Java
7.
Frequently Asked Questions
7.1.
How to create a thread in Java?
7.2.
How to run a code in a thread?
7.3.
How many threads are in Java?
7.4.
What are some common thread-related issues in Java?
7.5.
Can you start the same thread instance multiple times?
8.
Conclusion
Last Updated: Feb 4, 2025
Medium

Creating a Thread in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, creating a thread helps you to execute multiple tasks concurrently within a program. A thread is a lightweight unit of execution that can run independently, sharing the same memory space as other threads. With the help of threads, you can achieve parallelism and improve the performance of your applications. In this article, we will talk about threads in Java. We will discuss thread classes, methods used, runnable interface, and advantages.

Creating a Thread in Java

What is a Thread in Java?

Threads can be referred to as the lightweight process. They used less memory usage to create and exit the function. The main Thread is what makes the entire program. The secondary Thread is an outcome of the main Thread. 

A thread can be created by:

  • Implementing the Java.lang.Runnable interface.
  • Extending the Java.lang.Thread class.

Thread class in Java

Thread class gives the constructors and methods to create and perform several operations on a thread. Thread class extends the Object class and implements via Runnable interface.

Methods used in Thread class

  • public void run(): This method acts on a thread.
Thread my_thread = new Thread(MyRunnable());
my_thread.run();

 

  • public void start(): This starts the execution of the Thread.JVM and calls the run() method.
Thread t1 = new Thread();
t1.start();

 

  • public void sleep(long millisec): This method causes the current Thread to sleep with a specified time passed in the parameter.
Thread t = new thread();
t.sleep(10L*1000);

 

  • public String getName(): This Thread will return the name of the current Thread.
Thread t = new Thread(“My thread”);
String str = t.getName();

 

  • public void join(long millisec): This method waits for the Thread to die for the time specified in the parameter.
Thread the = new Thread();
the.join(100);

 

  • public void yield(): This Thread allows us to pause the current Thread and allows other threads to execute.
Thread tr = new Thread();
tr.yield();

 

  • public void stop(): This method will stop the Thread.
Thread tr = new Thread();
tr.stop();

Runnable Interface in Java

The second method mentions what type of code a thread should execute by creating a class that implements the Java.lang.Runnable interface. A Java Thread class can perform an object that implements this Runnable interface. The Runnable interface is a standard Java Interface that helps execute the Runnable interface that a Java Thread can achieve.

Runnable Interface Implementation

The best way to create the Java Runnable interface is by creating your own Java class that implements the Runnable interface. 

public class ExampleRunnable implements Runnable {
	public void run(){
	System.out. println("My Runnable");
	}
}

Advantages of Creating Threads

  • These threads are more lightweight, which means they take less time and resources to create a thread.
  • They help in sharing the data of their parent process.
  • When Thread communicates, it's much simpler than process communication.
  • Switching between threads is much cheaper than between processes.

Examples of Creating a Thread in Java

Example 1

  • Java

Java

public class Thread1 extends Thread {

public void run()
{
System.out.print("Welcome to Coding Ninjas.");
}
public static void main(String[] args)
{
Thread1 g = new Thread1();
g.start();
}
}
You can also try this code with Online Java Compiler
Run Code

Output

Welcome to Coding Ninjas.


In this example, we have developed a thread, and we have called an in-built function start to start the Thread.

Try it on online java compiler.

Example 2

  • Java

Java

public class Thread2 implements Runnable {

public static void main(String args[])
{
Thread2 cn = new Thread2();

Thread t = new Thread(cn, "CN");

t.start();

System.out.println(t.getName());
}
@Override public void run()
{
System.out.println("Inside blog");
}
}
You can also try this code with Online Java Compiler
Run Code

Output

CN
Inside blog

In this example, we have used one of the states of the threads, i.e., Runnable. We have created a Thread object and passed another object of the class. Then we have used the in-built method start() to start the Thread. Then we have used Override to implement the parent class.

Frequently Asked Questions

How to create a thread in Java?

A thread can be created by implementing the Runnable interface and overriding the run() method. A thread object is required to make a start() method. The Main Thread is required to initiate the program execution.

How to run a code in a thread?

There are several methods to specify which code to run in that Thread. These are Implement the interface java.lang.Runnable and pass the instance of the class implementing it to the Thread constructor.

How many threads are in Java?

In Java, the number of threads that can be created is limited only by the system resources and memory available. There is no fixed limit set by the Java language itself.

What are some common thread-related issues in Java?

Common thread-related issues in Java include race conditions, deadlocks, livelocks, thread starvation, and memory consistency errors. These arise due to improper synchronization, resource contention, or incorrect thread management, leading to unpredictable behavior and performance bottlenecks in concurrent applications.

Can you start the same thread instance multiple times?

No, a thread in Java cannot be started multiple times. Once a thread completes execution or is terminated, calling start() again on the same instance will throw an IllegalThreadStateException. Instead, a new thread instance must be created for each execution.

Conclusion

This article has covered how to create a thread in Java. We also gave a brief introduction about creating a Thread in Java. We also discussed the Java Threads and Thread class and various methods in Thread class with their syntax. We also discussed the Runnable interface and its implementation. We also discussed the advantages of a Thread class. We also discussed a few examples of creating Threads in Java.

Recommended Articles:

Live masterclass