Java - Concurrency / Threads


What is a Process in Concurrency / Threads ?

A process is a Unit of Execution that has it's own memory space.

When ever we run an application , we are starting a new process . 

Example : Running a Java FX application.
                 Running any Java console applications .  Nothing but we are starting a new process .

Each instance  of Java Virtual machine runs as a process (For most of them).


When ever two applications or process's are running , they have their own memory space or heap .

Applications or process's never share the memory / heap between them.

Thread :

What is a Thread ?

Thread is a Unit of Execution with in a Process . 

A process can have multiple threads .  Default in Java we have at least one thread called main thread.

For UI we have JavaFX application thread.

Our code runs in main thread or JavaFX app thread (UI) or threads that are explicitly provided by us.

Every Java application / process has multiple threads like memory management , Input / Output operations (I/O operations) .  Which we don't create , provided by java itself.

Unlike process's , threads can share files and memory of it's process / application.

In addition to process's memory thread has it's own memory called thread stack which can't be shared to other thread. Only that particular thread can access it's thread stack.


                                           Application runs as a  Process -> Heap Memory

                                                            Thread -> Thread Stack

                                                                          ->  Thread 1
                                                           Process   ->  Thread 2
                                                                           ->   Thread 3


Can't we run our entire application in one single thread as how we are running our application as a single process ?

Consider a scenario that you a fetching some data from an application . Where the application has several databases and also some information that it fetches from internet .

If we execute entire data fetching from all databases and internet in one main thread it will take more time or more latency because it act's in a linear fashion . User's of application may think app is down or got stuck by the time response is provided by main thread.

Using multiple threads , each thread will work on different databases and one thread will fetch data from internet and in the mean while main thread can provide status updates to user.

Finally all information can be collated and end result can be  displayed to User.

Example : you are watching a movie and parallelly downloading a file . Both are two tasks / threads running in parallel.


Java provides thread related classes to achieve concurrency .

Threads execution will depend on JVM and operating system on which they are running.











No comments:

Post a Comment

Featured Post

H1B Visa Stamping at US Consulate

  H1B Visa Stamping at US Consulate If you are outside of the US, you need to apply for US Visa at a US Consulate or a US Embassy and get H1...