Multithreading
Multithreading in Java
Multithreading is a process of executing multiple threads concurrently.
It is a combination of Multi + threading, where multi means multiple and thread means sub_process.
So, before understanding thread we should first understand what is process, as thread is a sub process means a part of a process.
Process:
Definition : Exceutable state of a program is known as process.
That means, everything that system (Computer) execute is known as process e.g, playing music, working with software like Microsoft word document, playing game etc are the examples of process.
Each process is a combination of one or more than one thread(s).
e.g; Microsoft office word document : It consists of multiple threads like one thread take the keyboard information for displaying content and simultaneously another thread do check on the content spelling.
So from above content we came to know that multiprocessing and multithreading both are part of multitasking.
Difference between Multiprocessing and MultiThreading :
Multiprocessing | Multithreading |
---|---|
Multiple process have their own memory location, as different software has different memory allocation in our. | As thread is a subpart of process it shares common memory allocation as that of process. |
As process are on different memory location therefore context switching time(time taken by processor to move from one process to another to allocate resources) is more. | Where threads has very small context switching time. |
Multithreading is mostly mostly used in games,animation.
Thread:
Definition : Thread is a light weighted sub-process.
Threads in java :
There are two ways to implement threads in java:
- By extending Thread class
- By implementing Runnable interface
Thread class is a predefined class in java heaving multiple methods to start and execute threads.
Whereas runnable is an Interface heaving run method only.
As java doesn’t support multiple inheritance with classes, it always provide an alternative to perform multiple inheritance with interface (as in case of multithreading it provide Runnable interface).
Example: with Thread class
Class ThreadDemo extends Thread
{
Public void run()
{
System.out.println(“Thread Execution code”);
}
Public static void main(String args[])
{
ThreadDemo a = new ThreadDemo()
a.start();
}
}
Example: with Runnable interface
Class ThreadDemo implements Runnable
{
Public void run()
{
System.out.println(“Thread Execution code”);
}
Public static void main(String args[])
{
ThreadDemo a = new ThreadDemo();
Thread t1 = new Thread(a);
t1.start();
}
}
Working of thread:
Threads complete its process or execution in five steps, known as life cycle of thread.
Java Thread state are as follows:
- New
- Runnable
- Running
- Non-Runnable
- Terminate
- New -> Thread is in new state when we create an instance of thread class.
e.g ThreadDemo a=new ThreadDemo(); //in case of extending thread;Thread t1=new Thread(a); //in case of implementing interface.
- Runnable -> Thread is ready to run but waiting for resource allocation.
i.e when we call start() method in program. - Running -> When resources are allocated to thread..
when start() method finds run() method // backend task - Non-Runnable/ blocked -> when thread is alive, but due to some reasons not eligible to run.
- Terminate -> Either Thread execution is completed or forcefully terminated.
When we want to create animated screens for android application, then we use multithreading concept. Android training in Chandigarh help you to create animated screen while creating android mobile applications.