Java Program to Create a Thread

06 Jun, 2021
Thread can be referred to as a lightweight process. Thread uses fewer resources to create and exist in the process; thread shares process resources. The main thread of Java is the thread that is started when the program starts. The slave thread is created as a result of the main thread. This is the last thread to complete execution.
 A thread can programmatically be created by:
Implementing the java.lang.Runnable interface.Extending the java.lang.Thread class.
Implementing the java.lang.Runnable interface.
Extending the java.lang.Thread class.
You can create threads by implementing the runnable interface and overriding the run() method. Then, you can create a thread object and call the start() method.
Thread Class:
The Thread class provides constructors and methods for creating and operating on threads. The thread extends the Object and implements the Runnable interface.
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
// start a newly created thread.
// Thread moves from new state to runnable state
// When it gets a chance, executes the target run() method
public void start()  
Runnable interface:
Any class with instances that are intended to be executed by a thread should implement the Runnable interface. The Runnable interface has only one method, which is called run().
// Thread action is performed
public void run() 
Benefits of creating threads :
When compared to processes, Java Threads are more lightweight; it takes less time and resources to create a thread.
Threads share the data and code of their parent process.
Thread communication is simpler than process communication.
Context switching between threads is usually cheaper than switching between processes.
Calling run() instead of start()
The common mistake is starting a thread using run() instead of start() method. 
  Thread myThread = new Thread(MyRunnable());
  myThread.run();  //should be start();
The run() method is not called by the thread you created. Instead, it is called by the thread that created the myThread. 
Example 1: By using Thread Class
Java
import java.io.*;class GFG extends Thread {    public void run()    {        System.out.print("Welcome to GeeksforGeeks.");    }    public static void main(String[] args)    {        GFG g = new GFG(); // creating thread        g.start(); // starting thread    }}
Welcome to GeeksforGeeks.
Example 2: By implementing Runnable interface
Java
import java.io.*;class GFG implements Runnable {    public static void main(String args[])    {        // create an object of Runnable target        GFG gfg = new GFG();          // pass the runnable reference to Thread        Thread t = new Thread(gfg, "gfg");          // start the thread        t.start();          // get the name of the thread        System.out.println(t.getName());    }    @Override public void run()    {        System.out.println("Inside run method");    }}
gfg
Inside run method
Java-Multithreading
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
Factory method design pattern in Java
Java Program to Remove Duplicate Elements From the Array