Creating a Thread in Java with Example

Threads as we have discussed are light- weight smallest part of process that can perform tasks simultaneously with other threads of the same process. In this article we will discuss how to create thread with examples in Java.

There are two ways by which thread can be created in Java:

  1. By extending Thread class
  2. By implementing Runnable interface.

Creating Thread By extending Thread class

First way of creating a multithreaded program is by extending Thread class. Thread class extends the Object class and implements Runnable interface. The Thread class provides many Constructors and methods that are used to do various operations on created threads. These constructors and methods are discussed below.

Constructors of Thread class

1. Thread()

This constructor creates a thread object having no predefined arguments.

2. Thread(String name)

This constructor creates a thread object having String argument.

3. Thread(Runnable r)

This constructor creates a thread object having argument r of type runnable

4. Thread(Runnable r,String name)

This constructor creates a thread object having two arguments of String and Runnable type.


Methods of Thread class:

1. public void run():

This method is used to perform needed action for a thread. This method is called by JVM after start() method is called.

2. public void start():

This method is called by thread object to start the execution of the thread. After this method is called, JVM automatically calls the run() method.

3. public void sleep(long milliseconds):

This method ceases the execution of currently executing thread for the time period as specified.

4. public void join():

This method causes the current thread (which calls this method) to wait until another thread to die.

5. public void join(long miliseconds):

This method causes the current thread (which calls this method) to wait for a thread to die for the specified miliseconds.

6. public int getPriority():

This method returns the priority (integer value) of the thread.

7. public int setPriority(int priority):

This method sets the priority of the thread. The priority is in terms of integer value from 1 to 10. Priority increases with integer value from 1 being lowest and 10 being highest.

8. public String getName():

This method returns the name of the calling thread.

9. public void setName(String name):

This method changes the name of the calling thread.

10. public Thread currentThread():

This method returns the reference of current thread which is executing.

11. public int getId():

This method returns the id of the calling thread.

12. public Thread.State getState():

This method returns the state of the calling thread.

13. public boolean isAlive():

This method returns true if the thread is alive, else returns false.

14. public void yield():

This method pauses the currently executing thread temporarily and allows other threads to execute.

15. public void suspend():

This method is used to put the thread in suspended state.

16. public void resume():

This method is used to resume the suspended thread.

17. public void stop():

This method is used to stop the running thread.

18. public boolean isDaemon():

This method returns true if the thread is a daemon thread, else returns false.

19. public void setDaemon(boolean b):

This method marks the thread as daemon or user thread.

20. public void interrupt():

This method interrupts the running thread.

21. public boolean isInterrupted():

This method returns true if the thread has been interrupted, else return false.

22. public static boolean interrupted():

This method returns true if the current thread has been interrupted, else return false.


Example of Creating Thread by extending Thread class

In the following example we have created a class that extends Thread class, we have divided this program into two steps that are discussed below:

Step 1: In this step run() method of Thread class is overrided. This method provides the starting point for the program and the core logic is written in this method.

Step 2: In this step, once Thread is created start() method is called which automatically calls the run() method.

public class MultithreadingDemo extends Thread{

//Step 1: Overriding the run() method

public void run(){

System.out.println(" This is Multithreading . . . ");

}

public static void main(String args[]){

MultithreadingDemo thread1=new MultithreadingDemo();

//Step 2: calling the start() method

thread1.start();
}
}

Output:

This is Multithreading . . .

Creating Thread By implementing Runnable interface

 Second way of creating a multithreaded program is by implementing runnable interface. This class provides only one method:

public void run()

This method is basically similar to method of Thread class, which is used to perform the basic action for a thread, core logic is written in this method.

Example of creating thread by implementing Runnable Interface.

 In the following example we have created a class that implements Runnable interface, we have divided this program into three steps that are discussed below:

Step 1: In this step run() method of Thread class is overrided. This method provides the starting point for the program and the core logic is written in this method. 

Step 2: In this step, Object of Thread class is created. That passes the object of that class which implements the Runnable interface.

Step 3: In this step, once Thread is created start() method is called which automatically calls the run() method.

public class MultithreadingDemo implements Runnable{

//Step 1: overriding run() method

public void run(){

System.out.println("this is multithreading...");

}
public static void main(String args[]){

MultithreadingDemo multiDemo=new MultithreadingDemo();

//Step 2: Creating object of Thread class

Thread thread1 =new Thread(multiDemo); 

//Step 3: Calling start method

thread1.start();
}
}

 Output:

this is multithreading...

One thought on “Creating a Thread in Java with Example”

  1. *Thread(Runnable r,String name)
    can you please tell me what i have to pass to this constructor at creating object and how.