To overcome the above problems we have to use Multi-Process Mechanism or Multitasking.
To execute applications by using Multitasking or Multiprocessing Mechanism we have to use the following components.
1.Main Memory: To load all the tasks.
2.Process Waiting Queue: To keep track of all process
3.Process Context Block: to manage status of all the processes execution.
4.Process Scheduler: It will take process from Process Waiting Queue and it will assign time stamps to each and every process in-order to execute.
In multiprocessing system, controlling is switched from one process context to another process context called as "Context Switching".
There are two types of Context Switching.
- Heavy Weight Context Switching: It is the context switching between two heavy weight components, it will take more memory and more execution time , it will reduce application performance. Each process has an address in memory. In other words, each process allocates a separate memory area. EX: Context switching between two Processes.
- Light Weight Context Switching: It is the context switching between two light weight components, it will take less memory and less execution time and it will improve application performance. Threads share the same address space. EX: Context switching between two threads.
Multitasking:
1. Process Based: executing several task simultaneously where each task is separate/independent programming process. Best suitable for process level.
2. Thread Based: executing several task simultaneously where each task is separate independent of same program and each independent path is called thread. Best Suitable for programming level.
Advantages of Java Multithreading:
- Doesn't block the user because threads are independent and you can perform multiple operations at the same time.
- You can perform many operations together, so it saves time.
- Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread.
There are two thread models to execute applications.
1. Single Thread Model: It will allow only one thread to execute application, it will follow sequential execution, it will increase application execution time and it will reduce application performance.
2. Multi Thread Model: It will allow more than one thread to execute application, it will follow parallel execution , it will reduce application execution time and it will improve application performance.