Operating Systems manage multiple processes on a computer. A process is a self-contained running program, with its own address space, that can be independently scheduled for execution. A thread is a sequence of execution within a process. A single process can have multiple concurrently executing threads. Thread can be thought of as a “lightweight” process. It does not have its own address space, but uses the memory and other resources of the process in which it executes.
The ability to do many tasks at the same time is multitasking. For example, printing a file, downloading a file or running different applications at the same time. It can be of two types:
Here, CPU gives attention to one process. This process must complete the allocated block of time, before any other process. These kinds of threads are called Selfish Threads.
In this case, if CPU has given attention to a process, it doesn't have to complete in the allocated block of time, if the next process of higher priority comes into picture. These threads are called Selfless Threads.
Advantages of Multithreading
It does not have its own address space, but uses the memory and other resources of the process in which it executes. In a program, you will have some part of the program tied to a particular event or resource and you don’t want to hang up the rest of the program because of that. So, you can create a thread associated with that event or resource and let it run independently of the main program.
1. Allows an application to perform multiple tasks concurrently.
2. A thread does not have its own address space, but uses the memory and other
resources of the process in which it executes. Therefore, the resources
required for a thread are substantially less than those required for a
3. The JVM manages threads and schedules them for execution. The time needed
to perform a context switch from one thread to another is very less than
that is required for performing such a change between processes.
4. Increased server responsiveness
Client-Server architecture allows multiple users on different machines to access information and applications simultaneously. In order to access server software or data, the client machine must put in a request to the server. The server must then process this request and return the desired information. However, there is no guarantee that the request will be processed right away, since the server may have multiple requests to fulfill. Without multithreaded programming, the server could only process one request at a time and would incapable of receiving other requests during this time.
Disadvantages of Multithreading
1. Slowdown, while waiting for shared resources
2. Additional CPU overhead required to manage threads
3. Data corruption, if multiple threads try to access shared data at the