What is Thread

A thread is a fork of a computer program in two or more concurrently running tasks. Let us try to understand it with example, 
  • Process: It is generally a single program in computer. Like, You need to get your house painted. So you hire a painting contractor and make a contract for painting your house. Now this painting contractor will get the house painted. So we can equate it with a process which, once start, will paint the home. Suppose you also want to change the flooring of your home, then you will hire some other contractor for this work. This will be another process which will do the flooring work. 
  • Threads: Now if the painting contractor paint the whole house himself single handedly, it will take him many days. But he as well as you want to get your work done ASAP. The solution is that contractor uses multiple sub-contractor under his control and use them to paint the house. These sub-contractors work simultaneously on different portion of the tasks. These sub-contractors can share the resources, i.e. will be using the resources from same pool. Now as multiple sub-contractors are working in parallel on multiple tasks, it will speed up the whole process and whole house will be painted in much lesser time. So these multiple sub-contractors are like threads in main process (painting contract).
  • Multiple Threads on Single Processor Machine: If multiple threads are running on a machine and machine is having only one processor, it can process only one thread at a time. So processor will schedule its time for each thread based on some scheduling algorithm (round robin, time slice etc), however there will always be only one thread in execution, and other threads will be just waiting in queue for their turn. However user experience is still like if there are multiple threads running in parallel. It is because the switching between the threads on processor is very very fast. So it creates an illusion that multiple threads are running simultaneously. It is like there are multiple sub-contractors, but with a single brush. Now it seems like multiple painters are working to complete the work ASAP, but actually only one painter is working at a time. The only difference is that every painter can be assigned a specific  job to do and hence every thread can complete its own job in separate sub-memory space. 
  • Multiple Threads on Multiple Processor Machine: Here comes actual multi-threading, where machine has multiple processors and is capable to run multiple threads at a time. Still there can be some threads who are waiting in queue for their turn, but machine can execute the number of threads equal to number of processors it has at a time. It is actually helping in reducing the time much faster. It is similar like most of the painters have their own brush, if not all. 
  • Context Switching: It is when processor switch its services from one thread to other thread, i.e. it unload the data of current running thread to some runtime memory cache, load the next thread data in itself and execute it. Context switching is comparatively lesser expensive in time, if it is among the threads because threads execute in same address space however processes uses their own address space. Hence unloading or reloading the data is comparatively expensive. It is same like if you want to swap the work between two sub-contractors or between two contractors. Changing the contractor is not easy, and it will cost you money and time both, however changing the work in sub-contractors is comparatively easy and have lesser cost. 

People who read this post also read :



0 comments:

Post a Comment