Introduction to multithreading

Java provides built-in support for multi-threaded programming. A multi-threaded program contains two or more part which can run independently. Each of these part is called Thread.

Multithreading in java is a process of executing multiple threads simultaneously.
The main aim of multi-threading is to archive multitasking.

What is Multitasking?

Executing several tasks Simultaneously is called multi-tasking.Multitasking is when multiple processes share common processing resources such as a CPU.

There are 2 types of multitasking
1) Process-Based multitasking.
2) Thread-Based multitasking.

Process-Based Multitasking:

Executing the several tasks simultaneously, where each task is a separate independent process, is called process based multitasking.
For example: While typing a program in editor we can listen audio songs by mp3 player in the system at the same time we can download a file from the internet. All these tasks are executing simultaneously and independent of each other. Hence, it is process based multitasking.

  Process -Based multitasking is best suitable at Operating System Level.

Thread-Based Multitasking:

Executing several tasks simultaneously where each task is a separate independent part of the same program is called Thread based multitasking and each part is called Thread.

  It is best suitable for programmatic level.

Advantage of Multitasking

Whether it is Thread-based or process based the main objective of multitasking is to improve the performance of the System by reducing the response time.

Advantages of Java 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 together so it saves time.

  Threads are independent so it doesn't affect other threads if exception occur in a single thread.

  Better use of CPU resource.

  Threads share the same address space and therefore can share both data and code.

What is Thread in java?

A thread is a lightweight sub process, a 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 shares a common memory area.

Difference between threads and processes

Following is tabular comparison between threads and processes in Java.

Process Thread
Every process has its own memory space, executable code and a unique process identifier (PID). Every thread has its own stack in Java but it uses process main memory and share it with other threads.
Processes use inter process communication techniques to interact with other processes. Threads communicate with each other by using Programming language construct like wait() and notify() methods in Java.
Context switching among processes are slower . Since all threads from the same process share same address space, inter-thread communication and context switching between them are much faster than inter-process communication and context switching between process.