Multithreading and Concurrency in Java Flashcards

1
Q

What is difference between Thread and Processes ?

A

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 .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between Concurrent and Parallel execution ?

A

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 ].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the Pros and Cons of multithreading ?

A

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 .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the Thread lifecycle ?

A
  1. Create a thread .
  2. make it Runnable [ ready to be executed ].
  3. make it Running [ executing it ].
  4. Waiting || Blocked .
  5. Terminated [ Killed ].
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the types of memory that Java deal with ?

A

Java deals with 2 types of memories :

  1. Stack memory : and from it’s name it is used to store data in LIFO [ store memory calls , primitives , method arguments and object reference ].
  2. Heap memory : is used to store objects itself , static values that are on class level , instance values [ primitives that are defined inside the class ].
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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 ]

A
  1. Are stored in Stack as they are related to the method call itself .
  2. all objects references are stored in stack .
  3. method parameters are also stored in stack as they are related to the method itself .
  4. method call frame is stored in stack [ stack memory calls ].
  5. Object created with new operator is stored in heap .
  6. instance variables are stored in heap as they maybe are used between multiple methods [ not coupled within any method frame].
  7. arrays references are stored in stack memory .
  8. arrays values are stored in heap .
  9. static values are stored in heap .
  10. static methods are also stored in heap .
  11. All class metadata are stored in heap memory .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly