Multitasking is a way of executing multiple tasks during the same period of time. Multiple tasks use common resources like CPU and main memory.
With a single CPU only one task can be running at one point in time, so the CPU uses context switching to perform multitasking. Context switch (Context means state) is the process of storing and restoring the state of a process so that execution can be resumed from the same point at a later time.
Types of Multitasking:
Multiprocessing is a type of multitasking based upon processes i.e. context switching is done in-between processes. For example: Typing in notepad, Listening to music, and downloading files from the internet at the same time. Here in the example, we can clearly see that all applications are independent. Multiprocessing is the type of multitasking that is handled at the operating system level.
A process has a self-contained execution environment i.e. allocates a separate memory area. Context switch time is more in the case of processes because the switch is done between different memory areas.
Multithreading is a type of multitasking based upon threads i.e. context switching is done in-between threads. In the case of multithreading, multiple independent tasks are executed simultaneously. These independent tasks are the part of same application. Multithreading is the type of multitasking that is handled at the program level.
A thread is a lightweight process. Thread uses the process’s execution environment i.e. memory area. Context switch time is less in the case of threads because the switch is done within the same process’s memory area.
A thread represents a flow of execution. Every thread has a job associated with it.
Note: A thread can’t exist without a process, it exists within the process.
Difference between process and thread in Java:
Java Multithreading tutorial:
- Thread life cycle in java.
- Way of creating thread in java.
- Which is a better way to create a thread in java?
- Methods of Thread class.
- Difference between thread start and run method.
- Why we call start method in thread?
- Can we override start method?
- Can we override run method?
- Is it possible to start a thread twice in java?
- Thread scheduling
- Thread priority
- Naming a thread
- What is join method in java?
- How to get current thread in java?
- Daemon thread
- Can we call run method directly in java?
- What is the difference between sleep and yield method?
- Inter-thread communication
- Synchronized method
- Static synchronization
- Synchronized block
Java concurrency tutorial
- Concurrency in java
- Concurrency issues
- Concurrency solutions
- Reentrantlock in java
- Readwritelock in java
- Semaphore in java
- Countdownlatch in java
- Exchanger in java
- Atomic variable in java
- Atomic boolean
- Atomic integer
- Atomic long
- Atomic integer array
- Atomic long array
- Thread pool in java
- Thread group in java
- Shutdown hook in java
- Executor framework
- Fork join in java
- Callable and Future
- Runnable vs Callable