I am writing this blog to put my understanding of Thread and Process in JAVA.
Before trying to know about thread directly it is good to have some knowledge about basic OS concepts like: Multiprocessing, Multitasking, Multiprogramming and MultiThreading. Refer the link to have detailed idea.
At very core, process is a set of instructions given to the CPU through main memory. This activity can be done in different ways, and this activity is called with different names like: light weighted process, main thread process, thread, daemon thread, background thread and many more. I will try to cover most of these terms in this blog.
In depth when ever we start any application on computer it creates a separate process for that particular program. Some application will allow to open another instance of it and some will not. As example in Android phone, if you have already gmail application open and running, and you try to open it again from its icon, it will not create another process for that, instead it will give you the access to the process that is already created. These kind of sandboxing features can implement by efficient thread programming.
If we focus on memory distribution throughout the process, each process has its own heap memory. As I mentioned process is a just set of instructions given to the CPU from main memory, the question comes what kind of instructions are these? So instruction means the sequence of programming commands. As example each program will have set of lines which will eventually treated as a command and will feed to the main stack memory for execution. Based on execution each programming element will create its own memory object in heap memory and its reference to stack. Have a look on my blog on memory management in JAVA for more detail.
Now the question is: If you can create multiple processes and everything with process then why thread is required?
Thread is nothing but a light weighted process. We can also say that process is a collection of multiple threads. Some time process only have single thread, I say that thread is entire process only rather than separate thread. Why?
Within a process we can have multiple threads like:
Main Thread has most importance in process. Main thread is a thread from which process execution starts. In program execution main thread will have highest priority at starting. Then we can switch to other threads within a system. Different applications perform different tasks within a main thread. In java applications at the time of execution process will create total three threads:
Deamon Thread is also known as a background thread which has lower priority compare to main thread when the execution begins. Scheduler on operating system performs context switching the execution as per thread priority. Have a look on my blog on Thread Lifecycle for more details.
As shown in figure [1] we have single threaded execution where each task executes one by one in sequence. So it is known as a 'sequential processing' of tasks. Here task 2 only executes when task 1 completes. So if we have long running task 2 then user cannot perform any further action until running task gets over. And so the reason is main thread which has been blocked by long running task2 process.
Before trying to know about thread directly it is good to have some knowledge about basic OS concepts like: Multiprocessing, Multitasking, Multiprogramming and MultiThreading. Refer the link to have detailed idea.
What is process?
At very core, process is a set of instructions given to the CPU through main memory. This activity can be done in different ways, and this activity is called with different names like: light weighted process, main thread process, thread, daemon thread, background thread and many more. I will try to cover most of these terms in this blog.
In depth when ever we start any application on computer it creates a separate process for that particular program. Some application will allow to open another instance of it and some will not. As example in Android phone, if you have already gmail application open and running, and you try to open it again from its icon, it will not create another process for that, instead it will give you the access to the process that is already created. These kind of sandboxing features can implement by efficient thread programming.
If we focus on memory distribution throughout the process, each process has its own heap memory. As I mentioned process is a just set of instructions given to the CPU from main memory, the question comes what kind of instructions are these? So instruction means the sequence of programming commands. As example each program will have set of lines which will eventually treated as a command and will feed to the main stack memory for execution. Based on execution each programming element will create its own memory object in heap memory and its reference to stack. Have a look on my blog on memory management in JAVA for more detail.
Now the question is: If you can create multiple processes and everything with process then why thread is required?
What is Thread? How does it differ from process?
Thread is nothing but a light weighted process. We can also say that process is a collection of multiple threads. Some time process only have single thread, I say that thread is entire process only rather than separate thread. Why?
Within a process we can have multiple threads like:
- Main Thread
- Deamon Thread / Background thread
Main Thread has most importance in process. Main thread is a thread from which process execution starts. In program execution main thread will have highest priority at starting. Then we can switch to other threads within a system. Different applications perform different tasks within a main thread. In java applications at the time of execution process will create total three threads:
- Main Thread
- 2 Deamon Threads: Garbage Collector and Scheduler.
Deamon Thread is also known as a background thread which has lower priority compare to main thread when the execution begins. Scheduler on operating system performs context switching the execution as per thread priority. Have a look on my blog on Thread Lifecycle for more details.
In android application Main Thread is also called as a UI thread. Here it is called a best practice to avoid heavy processing in UI thread to avoid ANR(Application Not Responding).
Why Thread is required?
As shown in figure [1] we have single threaded execution where each task executes one by one in sequence. So it is known as a 'sequential processing' of tasks. Here task 2 only executes when task 1 completes. So if we have long running task 2 then user cannot perform any further action until running task gets over. And so the reason is main thread which has been blocked by long running task2 process.
Here it comes the idea of thread and multithreading. Figure [1] has single threaded execution where Figure [2] have multi threaded execution. As shown in Figure [2] at second time interval we have two tasks executing at a same time, we can say that the 'parallel processing'. So from the beginning of task 2 system will create separate thread for task 3. It means task 3 will have its private stack memory to pass its instructions. By this functionality we can work through multiple tasks at a time. So logically to avoid long running process wait, we create background thread not to interrupt execution flow in Main Thread.
Logically only single task will be handled by processor but it will not wait until specific task gets over. It will switch the execution if there becomes a long interval due to some IO operation or something else.
I will like to take an example of file upload process in gmail application. We can do other activities along with the file uploading in gmail. So here for the process of file uploading, system have separate thread that doesn't block the main thread execution. If we do not implement separate thread for the file upload process, we cannot perform any other task when file is being uploaded.
No comments:
Post a Comment