Files
HW-09-Advanced-Multithreading/Answer.md
T

66 lines
3.8 KiB
Markdown

# Theoretical Questions
### Q1-What are atomic variables?
---
An atomic variable is a type of variable designed so that its update operations are indivisible and free of interference. This means that when an atomic variable is being updated, no other thread can change its value during the operation. However, in non-atomic variables, each operation can be divided; for example, the ++ operation is divided into three operations (reading, adding, and writing).
Also, for atomic variables, to change the value of the variables, you must use the methods within the class of that variable.
### Q2 - Four classes from the `java.util.concurrent.atomic`
---
1. AtomicInteger
2. AtomicLong
3. AtomicBoolean
4. AtomicReference
For example, AtomicInteger is used for working with integers, which is equivalent to the int variable in a non-atomic way.
and we can use some method to update it . for example `get()` , `set()` , `addAndGet()` , `intValue()` , ...
---
### Q3-Compare locks with atomic variables.
Atomic variables exist only for a specific type of data and Atomic variables only support simple operations, and for more complex operations, locks must be used.
For applications that do not require high concurrency, using a lock may be sufficient. However, in applications that require high performance and concurrency, atomic variables can be very useful.Using locks may encounter errors, for example when we forget to unlock a lock.
---
### Q4-High Contention
This situation indicates that your program has no correctness issues, but it faces structural bottlenecks in terms of throughput or scalability. Next, we will examine the three main factors that cause this problem in Java applications:
1. For example, you might have 100 threads, and due to the presence of a lock, at any moment at most 1 thread performs its work while the other 99 threads remain in the queue, which causes a slow queue.
2. Processors bring data from main memory to their cache in units called Cache Line (usually 64 bytes). If two different variables that are constantly modified by two different threads (on separate cores) reside in the same Cache Line, the hardware thinks the data is shared and constantly synchronizes the cores' caches with each other.
Problem in Java: Threads effectively wait for each other to update the cache, without actually accessing a shared resource.
3. In Java, GC is used to clear objects that are no longer used. For GC to be able to do its job correctly and to ensure that it does not mistakenly delete any live object, in many cases it needs to temporarily stop the entire program or a large part of it. This stoppage is called Stop-the-World (STW). ...
---
### Q5-Number of thread
1. When the CPU switches between threads or processes, it must save the current state (register values, program counter, stack, etc.) and load the next thread's state. This process slows down as the number of threads increases.
2. When multiple threads or processes want to access a shared resource simultaneously, they must wait for the other to finish its work. This waiting and locking cause slowdowns.
3. In multi-core CPUs, each core has its own cache. If a thread modifies a variable on core A, the copy of that variable in the caches of other cores must also be updated so that all copies are consistent. This cache coherence process (Cache Coherence Protocol) causes delays.
4. When we use locks to prevent the competition mechanism, it itself causes a waste of time.
---
### Q6-Deadlocks
In test we have a few thread and it is so simple but when we are in production we have most thread and a lot of Calculations
we have 2 solution
1. we can use more thread when we test our program to br similar to final status.
2. when we test our program we can use `sleep()` to change scheduling of threads.
---
# Finish