Introduction to Multithreading in Java

Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread.
Why to use multithreading?
  1. In Single process mechanism: only one task at a time by main memory. When there are multiple task it goes in sequential order.
  2. It will increase application execution time and it will reduce application performance.
To overcome the above problems we have to use Multi-Process Mechanism or Multitasking.
  1. IN Multitasking system is able to allow to load more than one task at a time in main memory and it able to allow more than one process to execute application, it will follow parallel execution , it will reduce application execution time and it will improve application performance.
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.
  1. 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.
  2. 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:
  1. Doesn't block the user because threads are independent and you can perform multiple operations at the same time.
  2. You can perform many operations together, so it saves time.
  3. 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.
Posted on by