What Is Multi Threading ? Why Do We Need Multi Threading In Java - Explained [THEORETICAL]

 What Is Multi Threading ? Why Do We Need Multi Threading In Java - Explained  [THEORETICAL]

Before we get in to Threading we need to know about the below terms first for better understanding.

What is a Multi Tasking ?

Multi Tasking is the way of performing more than one task at the same time.

e.g., We May be Listening to music on Music Player in our laptop while we are Tying a document in MS Word.

Here two tasks are going on in parallel. And our system is also able to handle it . This is called as Multi Tasking.

What is a Process ?

A Process is nothing but a Single task.

e.g., when we are using MS Word , the whole running of MS Word is called as a Process.

What is a Thread ?

A Thread is a unit of a Process  or  called as sub process. 

A Single Process may have many number of threads .

The process will create threads when needed to do things in parallel.

e.g., when I open MS Word and Tying something , the MS Word not only is typing what I type , It also checks the spelling automatically.

Here, Whole MS Word is a Process.

Displaying What we type in the word is a Thread.

Spelling Check for the word we enter is a Thread.

What is Multi Threading?

A Process with 'n' number of threads is called as Multi Threading.

The example we saw in the Thread is a e.g., of Multi Threading.

Multi Threading

Why We need Multi Threading ?

The processors in current generation are becoming more powerful that they can handle huge operations easily.

we would have heard about dual core, quad core , octa core and hexa core processors .

e.g., octa core means a processor with 8 cores , means 8 individual processing units.

In order to make use of these huge processing units we are thinking to split the work to all the cores and and make execution time faster.

e.g., we are trying to find the square of 1lakh numbers. Lets imagine it took 8 seconds for the code to complete the execution.[ we have not implemented multi threading] - so the process will get processed by only one core even though we have 8 cores.

but if we execute the same code with multi threading in a 8 core machine it may take only 1 second. This is the power of multi threading. 



Do The Default Java Code is Treated as a Thread ?

Yes , The java program that we run are executed as thread by default. i.e., the main is the default Thread.


You can see when we get an exception , we would have seen "Exception in thread main" it says that our main method is a thread.


Real Time Mobile Apps where you would have seen the application of multi threading :

E.g., Amazon

we would have been in the main page of the amazon app. Its what the Main Process is running.


In the above screen shot of home page of the app when I clicked on a deal , it will create a new thread. it will happens every time when we make a request. 



Incase if the app does not use multithreading until the  response is got the app will be not responding.

Similar thing happens with servlet request handling to. 

The games that we play like PUBG , Counter Strike etc., also will use multithreading as there are multiple things that are happening with in the game. 


Continue Reading :

https://iamafutureprogrammer.blogspot.com/2021/11/how-to-achieve-multi-threading-in-java.html

Previous Post
Next Post
Related Posts

0 comments:

Popular Posts