Thread Class in java

Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface.
To execute any thread or perform any operation with thread , a Thread object is required .


Constructors of java.lang.Thread class

Commonly used Constructors of Thread class are as follows :

  public Thread( )

  public Thread(String threadName )

  public Thread(Runnable r)

  public Thread(Runnable r , String threadName)

  public Thread(ThreadGroup g , String name)

  public Thread(ThreadGroup g , Runnable r)

  public Thread(ThreadGroup g , Runnable r , String name , long stackSize)

java.lang.Thread class methods

Commonly used methods of Thread class are as follows :

Method Description
public void run() This method is used to perform action for a thread. contains the main logic what has to perform.
public void start() This method is used to starts the execution of the thread.JVM calls the run() method on the thread.
public void sleep(long miliseconds) Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
public int setPriority(int priority) changes the priority of the thread.
public int getPriority() returns the priority of the thread.
public void setName(String name) This method is used to changes the name of the thread.
public void join() waits for a thread to die.
public void join(long miliseconds) waits for a thread to die for the specified miliseconds.
public Thread currentThread() returns the reference of currently executing thread.
public int getId() This mehtod returns the id of the thread
public boolean isAlive() It checks whether the thread is alive or not
public Thread.State getState() This method returns the state of the thread.
public void yield() causes the currently executing thread object to temporarily pause and allow other threads to execute. thread moves form Running to Runnable state.
public void suspend() is used to suspend the thread(depricated).
public void resume() is used to resume the suspended thread(depricated).
public void stop() is used to stop the thread(depricated).
public boolean isDaemon() tests if the thread is a daemon thread.
public void setDaemon(boolean b) marks the thread as daemon or user thread.
public void interrupt() This method is used to interrupt the current running thread.
public boolean isInterrupted() tests if the thread has been interrupted.
public static boolean interrupted() tests if the current thread has been interrupted.
public static void sleep(int millies) It is used to sleep the current running thread for specified period of time.

java.lang.Runnable interface

Runnable is a interface present in java.lang package. It contain only one method i. e. run()

public interface Runnable {
  public abstract void run();
}

  Note : In order to create user thread , Runnable interface need to be implemented in user defined class either directly or indirectly by extending java.lang.Thread class.

example of creating Thread . The Thread prints the table of 5.

class MyThread extends Thread{
   int n=5; 
	//overriding run() method	
	public void run()
	{
		for(int i=1;i<=10;i++)
		{
			System.out.println(n+" * "+i + " = "+ (n*i));
		}
	}
}

Creating our main application , so that we can start our thread.

class MainApp 
 {
 	public static void main(String[] args) {
 		MyThread mt=new MyThread();
 		 //start the thread by calling start() method.
 		mt.start();
 	}
 }
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Importance of Thread class start() method

To start a thread , the required mandatory activities (like - registering thread with thread scheduler ) will be performed automatically by thread class start() method .
Hence programmer is not responsible to perform this activity and he is just responsible to defined job of the thread.
Without executing start() method of thread class there is not chance of starting any new thread.
Let us see start() method of thread class :

class Thread
{
start()
{
1) Registering this thread with thread scheduler and perform initialization activities 
2) call run method
}
}

What happen , if we are not overriding run() method

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

class MyThread extends Thread
{
	
}
//Creating our main application , so that we can start our thread
 class MainApp 
 {
 	public static void main(String[] args) {
 		MyThread mt=new MyThread();
 		 //start the thread by calling start() method.
 		mt.start();
 	}
 }

 //compile and run this program. we will not get any output

can we overload run() method of Thread class ?

Yes , overloading of run() method is possible , but Thread class start() method will always call no argument run() method only .
Other run() method we have to call explicitly just like normal method class.

class MyThread extends Thread{
	public void run()
	{
		System.out.println("run()");

	}
	public void run(int x)
	{

		System.out.println("run(int x)");
	}
	public void run(String str)
	{
		System.out.println("run(String str)");
	}
}

//create Another class which contain main() method
class ThreadDemo{
	public static void main(String[] args) {
		MyThread t=new MyThread();
		//starting the thread
		t.start();

	}
}
run()

can we override start() method of Thread class ?

Yes , overriding of start() method is possible , but if we override start() method then start() will be executed just like a normal method call and no new thread will be created.

class MyThread extends Thread{
	public void run()
	{
		System.out.println("run()");

	}
	public void start()
	{
		System.out.println("This is start method");
	}
}

//create Another class which contain main() method
class ThreadDemo{
	public static void main(String[] args) {
		MyThread t=new MyThread();
		//starting the thread
		t.start();

	}
}
This is start method