In this java tutorial, we will learn about multithreading in java, how to create a thread, run multiple threads, the life cycle of a thread, and many more important methods of Thread class in Java with an example.
What is a Thread?
In computer science, a thread is a small set of instructions that are executed independently by the CPU. The thread is a small part of a program and by default, every program in java is a thread, it’s referred to as a main thread.
Multithreading in Java.
The process of executing a single task is a thread. Processing or executing two or more parts of a program concurrently to maximize utilization of the CPU, refer as a multithread.
An example of multithreading is a word processor. Whenever you use MS Word to write something, at the time of typing multiple threads are used, to display, check the spelling & grammar, autosave, etc. These all are happening concurrently, internally to perform the task.
Example of a Thread.
class Demo
{
public static void main(String args[])
{
//get info of present running thread and print name of the thread
Thread t=Thread.currentThread();
System.out.println(t.getName());
}
}
In the above example, Thread is a predefined class in Java. The currentThread() is a method of the Thread class, it returns the present running Thread as an object. The getName() method returns the name of the thread.
The setName(), to change the name of a Thread.
We can change the name of a thread in Java using the setName() method of the Thread class.
class Demo
{
public static void main(String args[])
{
//get info of the present running thread and print name.
Thread t=Thread.currentThread();
//we can change the name of a thread, using setName() method.
t.setName("Coder_mantra");
System.out.println(t.getName());
}
}
Pausing Execution – sleep() method in Java.
In Java, we can pause the execution of the current thread for a specific period using the sleep() method. At that time, another thread may execute, if you have multiple threads.
class Demo
{
public static void main(String args[])
{
Thread t=Thread.currentThread();
try
{
for(int n=1; n<=10; n++)
{
System.out.println(n);
t.sleep(1000);
//sleep() pause the execution for a second
//1000 as parameter is equivalant to 1 second
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
In the above example, it will print 1 to 10 but delay by one second. If we put 5000 as a parameter, then the delay will be 5 seconds.
Note: The sleep() method must be within try and catch block. It may throw an exception of the InterruptedException, if the thread is interrpted by another thread.
How to create Threads in Java.
Here is an important question, how to create Threads in Java? In Java, we have two ways to create the threads.
- By extending Thread class.
- Implementing Runnable interface.
By extending Thread class.
We can create a thread by extending the Thread class in Java. Thread class has a predefined run() method, you need to override that method and write the code we want to execute whenever the thread is constructed.
//creating first thread
class A extends Thread
{
public void run()
{
try
{
for(int n=1; n<=10; n++)
{
System.out.println("A : "+n);
sleep(1000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
//creating Second thread
class B extends Thread
{
public void run()
{
try
{
for(int n=1; n<=10; n++)
{
System.out.println("B : "+n);
sleep(1000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
//creating third thread
class C extends Thread
{
public void run()
{
try
{
for(int n=1; n<=10; n++)
{
System.out.println("C : "+n);
sleep(1000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Demo
{
public static void main(String args[])
{
A a=new A();
B b=new B();
C c=new C();
//call the start() method, it internally execute the run()
a.start();
b.start();
c.start();
}
}
Implementing Runnable interface.
We can also create the threads with the help of the Runnable interface in Java. As we know, when we implement an interface, its method must be overridden. The runnable interface also has a run() method which we have to override, and for those tasks which we want to execute as multithreaded, we will write them in the run() method.
class Demo implements Runnable
{
public void run()
{
for(int n=1; n<=10; n++)
{
try
{
System.out.println(n);
Thread.sleep(1000);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
public static void main(String a[])
{
Demo d = new Demo();
Thread t = new Thread(d); //creating thread
t.start();
}
}