How to make classes Threadable using Thread Class


How to make classes as Threadable using Thread Class

In this tutorial, we will discuss,

What are threads?

Difference between Thread and Process.

How to make classes Threadable using Thread class?

Video Tutorial:

What are Threads…?

One of the exciting features of the Windows operating system is that – It allows the user to handle multiple tasks together. This facility in Windows operating system is called multitasking.

In Java, we can write the programs that perform multitasking using the multithreading concept. Thus Java allows having multiple control flows in a single program by means of multithreading.

Definition of thread:

Thread is a tiny program running continuously. It is sometimes called a lightweight process. But there lie differences between thread and process.

Difference between Thread and Process.

Thread is a lightweight processThe process is a heavyweight process
Thread does not require separate address space for its execution. It runs in the address space of the process to which it belongs.Each process requires a separate address space to execute.

How to make classes Threadable using Thread class?

In Java, we can implement the thread programs using two approaches –

  1. Using Thread class
  2. Using runnable interface (Click here to read)
How to make classes Threadable using Thread class?

As given in the above Fig., there are two methods by which we can write the Java thread programs one is by extending thread class and the other is by implementing the Runnable interface.

1. The run() method is the most important method in any threading program. By using this method the thread’s behavior can be implemented. The run method can be written as follows –

public void run()
     Statement for implementing thread

2. For invoking the thread’s run method the object of a thread is required. This object can be obtained by creating and initiating a thread using the start() method.

Extending Threads

The Thread class can be used to create a thread.

Using the extends keyword your class extends the Thread class for the creation of thread.

For example, if I have a class named A then it can be written as

class A extends Thread

Various commonly used methods during thread programming are as given below –

start() – The thread can be started and invokes the run method.

run() – Once the thread is started it executes in the run method.

setName() – We can give the name to a thread using this method.

getName() – The name of the thread can be obtained using this name.

join() – This method waits for a thread to end

The simple Java Program to how to make classes Threadable using Thread class?

public interface Interface1 
class MyThread extends Thread 
	public void run() 
		System.out.println("Thread is created!!!"); 
class ThreadProg 
	public static void main(String args[]) 
		MyThread t =new MyThread(); 


Thread is created!!!


This tutorial discusses, How to make classes Threadable using Thread Class. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

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