Java Concurrency Basics
By AmarSivas | | Updated : 2020-12-24 | Viewed : 97 times
Table of Contents:
What is Concurrency
Concurrency is nothing execution of multiple tasks by many numbers of threads with different core CPUs. So once Thread on core CPU complete the independent subtask and then core CPU can be available for another one. So all the subtasks which are individuals will be executed in a fast way so that all tasks will be completed in a fast manner.
But So many challenges will come into the picture while in concurrent execution. Concurrent way of execution Let allow the program for execution in a fast manner. It utilizes the MultiCore of the CPU to give the best performance. If you are going to sequential execution it may consume time compare to the Concurrence execution.
Limits of Concurrency
The performance will be directly proportional to the number of available core CPUs. When core CPUs are more than all tasks to be executed then execution will be done in less time.
What is Multithreading
The first topic which should be covered is
In the Java application, Many threads created and managed for completing the tasks. Each thread will get allocated by one of the CPUs to complete the task. So the thread is under the control of this CPU to fulfill the task. Once the task is completed the thread will die and the CPU will be free for taking the other thread to finish the task. The remaining threads are also allocated by the remaining CPUs based on their availability. some times one can take more number threads. Here the CPU is required to switch between multiple threads for thread allocation.
Process Vs Thread
What is the difference between process and thread? It is very important to know both of them.
Program execution called as the process. It is a heavyweight for executing any program even it takes time for shut downing. Technically speaking process control the other program.
A Thread is a part of the process. It is lightweight when compared to the process. The thread does not take much time for creation as well as for destroying. Generally, a process may contain many threads.
Differences Between Thread And Process
The process is heavyweight which means for creation and termination of the process is required some time where not required much in threads.
A context switch is heavy in the case of the process but not in threads. In the process, the switching process uses the operating system whereas it is not required in threads switching.
The process requires more resources whereas thread requires fewer resources for executing the program.
The process is less efficient to communicate one to another. in contrast threads are very very efficient.
Blocking of one process can be impacted by other processes until the first process is getting unblocked but in threads not like the process. Once thread blocking will never be impacted on others.
Multithreading is very beneficial when each thread handles an independent task. So performance will get improved much. The ideal scenario is when independent tasks exist in the application then Multithreading is one of the very good options to complete the application.