Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

February 11, 2016 Categories: Multithreading. No Comments on Creating Threads

In this article we will look at creating threads, a single thread and multiple threads using Thread class and Runnable interface along with sample programs.

We can create a thread in a Java program in two ways, either by:

  1. Extending Thread class or
  2. Implementing Runnable interface

 

Creating a Single Thread

 

Implementing Runnable Interface

Following are the steps that must be performed for creating a thread using Runnable interface.

  1. Create a class and implement Runnable interface.
  2. Implement run() method. This method contains the thread code and is the entry point for every thread.
  3. Invoke start() method. This method calls the run() method.

Below is a program which creates a single thread using Runnable interface:

Output of the above program is:

Child thread: Thread[Thread-0,5,main]
Main thread: 1
Child thread: 1
Child thread: 2
Main thread: 2
Child thread: 3
Child thread terminated
Main thread: 3
Main thread terminated

 

Extending Thread Class

Following are the steps that must be performed for creating a thread using Thread class:

  1. Create a class and extend Thread class.
  2. Override the run() method.
  3. Invoke start() method.

Below is a program which creates a single thread using Thread class:

Output of the above program:

Child thread: Thread[Thread-0,5,main]
Main thread: 1
Child thread: 1
Child thread: 2
Child thread: 3
Main thread: 2
Child thread terminated
Main thread: 3
Main thread terminated

Note: Output for the above example programs might vary a little when you run the above code on your machine as which thread executes when is under the control of JVM.

 

Creating Multiple Threads

As we already know how to create a single thread, you can create multiple threads by simply creating multiple objects of your thread class as shown in the example program below:

Output of the above program is:

Child thread: Thread[First Thread,5,main]
Child thread: Thread[Second Thread,5,main]
First Thread: 1
Child thread: Thread[Third Thread,5,main]
Second Thread: 1
Main thread: 1
Third Thread: 1
First Thread: 2
Second Thread: 2
Third Thread: 2
First Thread: 3
Second Thread: 3
Main thread: 2
Third Thread: 3
First Threadis terminated
Second Threadis terminated
Third Threadis terminated
Main thread: 3
Main thread terminated

 

How to choose between Thread and Runnable?

You may think that why Java is providing two options (Thread class and Runnable interface) for creating threads? Choose Thread class when you want to override some of the methods available in the Thread class.

Choose Runnable interface if you don’t want to override any methods or if your class have to extend some other class (multiple inheritance of classes is not supported).

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me