Thread class
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();

You can also try this code with Online Java Compiler
Run Code
- public void start(): This starts the execution of the Thread.JVM and calls the run() method.
Thread t1 = new Thread();
t1.start();

You can also try this code with Online Java Compiler
Run Code
- 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);

You can also try this code with Online Java Compiler
Run Code
- public String getName(): This Thread will return the name of the current Thread.
Thread t = new Thread(“My thread”);
String str = t.getName();

You can also try this code with Online Java Compiler
Run Code
- 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);

You can also try this code with Online Java Compiler
Run Code
- public void yield(): This Thread allows us to pause the current Thread and allows other threads to execute.
Thread tr = new Thread();
tr.yield();

You can also try this code with Online Java Compiler
Run Code
- public void stop(): This method will stop the Thread.
Thread tr = new Thread();
tr.stop();

You can also try this code with Online Java Compiler
Run CodeRunnable Interface
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");
}
}

You can also try this code with Online Java Compiler
Run CodeAdvantages 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
Implementation 1
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 CodeOutput
Welcome to Coding Ninjas.

You can also try this code with Online Java Compiler
Run Code
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.
Implementation 2
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 CodeOutput
CN
Inside blog

You can also try this code with Online Java Compiler
Run CodeIn 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.
Also see, Duck Number in Java and Hashcode Method in Java
FAQs
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.
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.
If you want to practice this concept, please visit our website Code Studio Library.