Multitasking:- Execution of more than one task simultaneously is called multi-tasking.
Multi-threaded programming is a way to achieve multitasking. In this case more than one method of the running program can be executed at a time.
Multitasking is achieved the utilized idle time of CPU. It’s controlled by the operating system.
It can be achieved by following two ways:-
1. By using processes
2. By using threads.
Process:- Running instance of a program is known as a process.
Threads:- It is the smallest unit of processing.It is also known as a sub-process.Processes are heavy weight while threads are light weight.
Processes occupy a separate location in memory while threads shares the same location in memory.
Process based multi-tasking:- It is also known as multi-processing. On process based multi-tasking,multiple process are executed simultaneously. Multitasking in Operating system is a process based multi-tasking.
Thread based multi-tasking:- It is also known as multi-threading. In thread based multi-tasking,multiple threads executed simultaneously.Each thread performs a particular task.This type of programming is called multi-threaded programming.
*java.lang.Thread class:- In java every thread is represented in the form of an object.this object must be an instance of Thread class or its child class.
Class MyThread extends Thread
Thread th=new Thread();
MyThread th= new MyThread();
When Execution of thread is started,its calls following methods:-
Public void run()
Above method must be overridden in the multi-threaded program.in this method you will have to write multi-tasking code.Prototype of this method has been declared in following interface.
Thread class has already implemented Runnable interface, so you do not need to implement this interface with multi-threaded program.But it can be implemented.
Class MyThread implement Runnable
Public void run()
//code to perform task by thread
Life-cycle of a thread:- Life cycle of a thread managed by Thread scheduler. It is a component of the JVM.
The Operating system controls the thread scheduler. A Thread can be in one of the following five phases at a time:
1. New state (Born state)
2. Runnable state
3. Running state
4. Non-Runnable state
5. Terminated state()
1.New state:- When an object of thread class or its child class is created that thread entered into new states.
2. Runnable state:- When the state's method of Thread class is executed on a new thread object then that thread entered into runnable state. More than one thread can be in this state at a time.
3. Running state:- One of the runnable thread will chosen by Thread scheduler (OS) to start execution. That Thread entered into Running state. In this case Run method is executed by that Thread. Only can Thread can be in Running state at a time.
4. Non-Runnable state:- Execution of a Running Thread can be suspended. In this case Thread scheduler sends that thread into non-runnable state.
5. Terminated state:- When Execution of Run method is completed, Thread scheduler sends that thread into terminating state.