16. How do you call wait()
method? using if block or loop? Why?
wait() method should always be called using loop because it may happen that until thread gets CPU to start running again the condition might not hold true, so it is always better to check condition before proceeding.
wait() method should always be called using loop because it may happen that until thread gets CPU to start running again the condition might not hold true, so it is always better to check condition before proceeding.
17. What is false sharing in the
context of multi-threading? How it can be avoided?
When
threads on different processor modify variables that reside on same cache line
then false sharing occurs. It is very hard to detect because the thread may be
accessing completely different global variables that happen to be relatively
close together in memory. The primary way to avoid false sharing is careful
code review and aligning your data structure with the size of a cache line.
18. What is busy spin? Why should
you use it?
It is one of the technique to wait for events without releasing CPU. If the thread is paused and resumed in some other core, we may lose the data in CPU cache.. So, if you are working on low latency system where your order processing thread currently doesn't have any order, instead of sleeping or calling wait(), you can just loop and then again check the queue for new messages. It's only beneficial if you need to wait for a very small amount of time e.g. in micro seconds or nano seconds.
It is one of the technique to wait for events without releasing CPU. If the thread is paused and resumed in some other core, we may lose the data in CPU cache.. So, if you are working on low latency system where your order processing thread currently doesn't have any order, instead of sleeping or calling wait(), you can just loop and then again check the queue for new messages. It's only beneficial if you need to wait for a very small amount of time e.g. in micro seconds or nano seconds.
19. Can we create an Immutable
object, which contains a mutable object?
Yes, you can , just do not to share the reference of the mutable component, instead, you should return a copy of it if you have to. Most common example is an Object which contain the reference of java.util.Date object.
Yes, you can , just do not to share the reference of the mutable component, instead, you should return a copy of it if you have to. Most common example is an Object which contain the reference of java.util.Date object.
20. What is the difference
between stack and heap in Java?
Stack
memory is used to hold method frames while heap memory is used for memory
allocation of objects.
Stack memory is not shared between multiple threads
whereas heap memry is shared among all threads in JVM.
Usually, stack memory is much smaller than heap
memory.
No comments:
Post a Comment