Creating a Thread in Java
- Keshari Abeysinghe

- Aug 3, 2020
- 3 min read
Updated: Dec 9, 2020
To create a thread, Java provides a class Thread and an interface Runnable both are located into java.lang package.We can create thread either by extending Thread class or implementing Runnable interface. Both includes a run method that must be override to provide thread implementation.
Thread class provide constructors and methods to create and perform operations on a thread.
Commonly used constructors in Thread class
Thread()
Thread(String name)
Thread(Runnable r)
Thread(Runnable r,String name)
Commonly used methods in Thread class
Creation of thread object never starts execution, we need to call 'start()' method to run a thread. Examples gives you more details. Other methods supported by Threads are given below.
join(): It makes to wait for this thread to die. You can wait for a thread to finish by calling its join() method.
sleep(): It makes current executing thread to sleep for a specified interval of time. Time is in milli seconds.
yield(): It makes current executing thread object to pause temporarily and gives control to other thread to execute.
notify(): This method is inherited from Object class. This method wakes up a single thread that is waiting on this object's monitor to acquire lock.
notifyAll(): This method is inherited from Object class. This method wakes up all threads that are waiting on this object's monitor to acquire lock.
wait(): This method is inherited from Object class. This method makes current thread to wait until another thread invokes the notify() or the notifyAll() for this object.
Java Thread by Implementing Runnable Interface
Runnable Interface
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run().
run() method used to perform action for a thread
start() method is used to start a newly created thread.Then thread moves from New state to the Runnable state.When the thread gets a chance to execute, its target run() method will run.
A Thread can be created by extending Thread class also. But Java allows only one class to extend, it wont allow multiple inheritance. So it is always better to create a thread by implementing Runnable interface. Java allows you to implement multiple interfaces at a time.By implementing Runnable interface, you need to provide implementation for run() method.To run this implementation class, create a Thread object, pass Runnable implementation class object to its constructor. Call start() method on thread class to start executing run() method.Implementing Runnable interface does not create a Thread object, it only defines an entry point for threads in your object. It allows you to pass the object to the Thread(Runnable implementation) constructor.
class Student implements Runnable{
public void run(){
System.out.print(" Hello World! ");
}
public static void main(String args[]){
Student stu= new Student();
Thread th=new Thread(stu);
th.start();
}
}Expected Output
Hello World! Java Thread by Expanding Thread Class
A thread can be created in java by extending Thread class, where you must override run() method.
Call start() method to start executing the thread object.
class Student extends Thread{
public void run()
{
System.out.print("Hello World!");
}
public static void main(String args[])
{
Student stu = new Student();
stu.start();
}
}Expected Output
Hello World! The main thread
When a programme starts to run, its code starts from the main method .Therefore , the JVM creates a thread to start executing the code present in main method. This thread is called as main thread. Although the main thread is automatically created, you can control it by obtaining a reference to it by calling currentThread() method.
Lets begin to learn more about the Multithreading ......




Comments