Multithreading
Multithreading in java is a process of executing multiple threads simultaneously. Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.
But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.
Thread is executed inside the process. There is context-switching between the threads. There can be multiple processes inside the OS and one process can have multiple threads.

Advantages of Multithreading :
- It doesn't block the user because threads are independent and you can perform multiple operations at same time.
 - You can perform many operations simultaneously so it saves time.
 - Threads are independent so it doesn't affect other threads if exception occur 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 by two ways:
- Process-based Multitasking(Multiprocessing)
 - Thread-based Multitasking(Multithreading)
 
| Multiprocessing | Multithreading | 
|---|---|
| Each process have its own address in memory i.e. each process allocates separate memory area. | Threads share the same address space. | 
| Process is heavyweight. | Thread is lightweight. | 
| Cost of communication between the process is high. | Cost of communication between the thread is low. | 
| Context-switching require some time for saving & loading registers, memory maps, updating lists etc. | Context-switching between the threads takes less time than process. | 
NOTE : Context switching (aka process/task switching) is the switching of the CPU (central processing unit) from one process or thread to another.
Life Cycle of a Thread
The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:
- New - The thread is in new state if you create an instance of Thread class but before the invocation of start() method.
 - 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.
 - Running - The thread is in running state if the thread scheduler has selected it.
 - Non-Runnable (Blocked) - This is the state when the thread is still alive, but is currently not eligible to run.
 - Terminated - A thread is in terminated or dead state when its run() method exits.
 
NOTE : 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.

Creating a Thread
There are two ways to create a thread:
- By extending Thread class
 - By implementing Runnable interface.
 
Thread class: Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface.
Commonly used Constructors of Thread class:
- Thread()
 - Thread(String name)
 - Thread(Runnable r)
 - Thread(Runnable r,String name)
 
Commonly used methods of Thread class:
- public void run(): is used to perform action for a thread.
 - public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
 - public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
 - public void join(): waits for a thread to die.
 - public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
 - public int getPriority(): returns the priority of the thread.
 - public int setPriority(int priority): changes the priority of the thread.
 - public String getName(): returns the name of the thread.
 - public void setName(String name): changes the name of the thread.
 - public Thread currentThread(): returns the reference of currently executing thread.
 - public int getId(): returns the id of the thread.
 - public Thread.State getState(): returns the state of the thread.
 - public boolean isAlive(): tests if the thread is alive.
 - public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.
 - public void suspend(): is used to suspend the thread(deprecated).
 - public void resume(): is used to resume the suspended thread(deprecated).
 - public void stop(): is used to stop the thread(deprecated).
 - public boolean isDaemon(): tests if the thread is a daemon thread.
 - public void setDaemon(boolean b): marks the thread as daemon or user thread.
 - public void interrupt(): interrupts the thread.
 - public boolean isInterrupted(): tests if the thread has been interrupted.
 - public static boolean interrupted(): tests if the current thread has been interrupted.
 
Runnable interface: The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run(). public void run(): is used to perform action for a thread.
Starting a thread: start() method of Thread class is used to start a newly created thread. It performs following tasks:
- A new thread starts(with new callstack).
 - The thread moves from New state to the Runnable state.
 - When the thread gets a chance to execute, its target run() method will run.