Multithreading and Concurrency in Java Flashcards
What is difference between Thread and Processes ?
Process is an execution env for a program [ as in task manager on windows ] that contain the whole instructions for program execution .
Thread is a smallest unit of the Process execution , it is a small part of a process itself that can be executed either concurrently or in parallel .
What is the difference between Concurrent and Parallel execution ?
Concurrent means that the whole threads are sharing the same processing unit by time sharing technique [ if one started to execute , the other one is blocked ] .
Parallel means that more than 1 thread is executed within the same unit of time over multiple processing units [ each thread on it’s own CPU ].
What are the Pros and Cons of multithreading ?
PROS :
1. performance increase .
2. better cpu utilization .
3. responsive app [ solve freezing problem in apps ].
Cons :
1. Data inconsistency .
2. Not easy to design multi threaded app .
3. multi threading env maybe more expensive on CPU because of the need of loading the context of each thread while calling to to continue execution .
What is the Thread lifecycle ?
- Create a thread .
- make it Runnable [ ready to be executed ].
- make it Running [ executing it ].
- Waiting || Blocked .
- Terminated [ Killed ].
What are the types of memory that Java deal with ?
Java deals with 2 types of memories :
- Stack memory : and from it’s name it is used to store data in LIFO [ store memory calls , primitives , method arguments and object reference ].
- Heap memory : is used to store objects itself , static values that are on class level , instance values [ primitives that are defined inside the class ].
Tell me where each type is stored [ Stack , Heap ] ?
1. Local Primitives variables .
2. local object reference .
3. method parameter .
4. method call frame [ method call context ].
5. Object created with new operator ?
6. instance variable .
7. Arrays reference
8. Arrays values .
9. static values .
10. static methods .
11. class metadata [bytecode , info , method definitions ]
- Are stored in Stack as they are related to the method call itself .
- all objects references are stored in stack .
- method parameters are also stored in stack as they are related to the method itself .
- method call frame is stored in stack [ stack memory calls ].
- Object created with new operator is stored in heap .
- instance variables are stored in heap as they maybe are used between multiple methods [ not coupled within any method frame].
- arrays references are stored in stack memory .
- arrays values are stored in heap .
- static values are stored in heap .
- static methods are also stored in heap .
- All class metadata are stored in heap memory .