Multithreading in Java
Multithreading in Java is a process of executing multiple threads simultaneously.
A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and
multithreading, both are used to achieve multitasking.
Advantages of Java Multithreading
1. You can perform many operations together, so it saves time.
2. Threads are independent, so it doesn't affect other threads if an exception occurs in a
single thread.
Multitasking
Multitasking is a process of executing multiple tasks simultaneously. We use multitasking
to utilize the CPU. Multitasking can be achieved in two ways:
o Process-based Multitasking (Multiprocessing)
o Thread-based Multitasking (Multithreading)
What is Thread in java
A thread is a lightweight subprocess, the smallest unit of processing. It is a separate path of
execution.
Threads are independent. If there occurs exception in one thread, it doesn't affect other
threads. It uses a shared memory area.
Life cycle of a Thread (Thread States)
A thread can be in one of the five states. According to sun, there is only 4 states in thread
life cycle in java new, runnable, non-runnable and terminated. There is no running state.
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated
1) New
The thread is in new state if you create an instance of Thread class but before the
invocation of start() method.
2) Runnable
The thread is in runnable state after invocation of start () method, but the thread scheduler
has not selected it to be the running thread.
3) Running
The thread is in running state if the thread scheduler has selected it.
4) Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not eligible to run.
5) Terminated
A thread is in terminated or dead state when its run () method exits.
Thread t=new Thread();
Implement of Thread:
Thread class:
Thread class provide constructors and methods to create and perform operations on a
[Link] class extends Object class and implements Runnable interface.
Starting a thread:
The start() method of Thread class is used to start a newly created thread. It performs the
following tasks:
o A new thread starts(with new callstack).
o The thread moves from New state to the Runnable state.
o When the thread gets a chance to execute, its target run() method will run.
How to create thread
There are two ways to create a thread:
1. By extending Thread class
2. By implementing Runnable interface.
Class singletThread extends Thread
{
Start ()
run()
}
Example:
import [Link].*;
class Single extends Thread
{
public void run()
{
for(int i=0;i<=5;i++)
{
[Link](i);
}}}
class ST
{
public static void main(String arg[])
{
Single t=new Single();
[Link]();
}}
Example 2:
import [Link].*;
class Single implements Runnable
{
public void run()
{
for(int i=0;i<=5;i++)
{
[Link](i);
}}}
class STR
{
public static void main(String arg[])
{
Single s=new Single();
Thread t=new Thread(s);
[Link]();
}}
Methods of Thread:
Start()
Run()
Sleep(milliseconds)
Join()
getID()
getName()
SetName(String)
GetPriority()
setPriority()
Example 1:
import [Link].*;
class Th extends Thread
{
public void run()
{
for(int i=0;i<=5;i++)
{
[Link](i);
}}}
class TMethods
{
public static void main(String arg[])
{
Th t1=new Th();
[Link]("ID="+[Link]());
[Link]("Name of Thread is"+[Link]());
[Link]("Swathi");
[Link]("Name ofThread after changing its name is"+[Link]());
[Link]("Priority of Thread is"+[Link]());
[Link](10);
[Link]("Priority of Thread after changing is"+[Link]());
[Link]();
}}
Example 2:
import [Link].*;
class Th extends Thread
{
public void run()
{
for(int i=0;i<=5;i++)
{
[Link](i);
}}}
class TMethods
{
public static void main(String arg[])
{
Th t1=new Th();
Th t2=new Th();
[Link]("ID="+[Link]());
[Link]("Name of Thread is"+[Link]());
[Link]("Swathi");
[Link]("Name ofThread after changing its name is"+[Link]());
[Link]("Priority of Thread is"+[Link]());
[Link](1);
[Link]("Priority of Thread after changing is"+[Link]());
[Link]();
[Link]();
}}