Câu hỏi phỏng vấn Java
Câu hỏi

What is difference between fail-fast and fail-safe?

Câu trả lời

The difference between fail-fast and fail-safe iterators in Java primarily revolves around how they handle concurrent modifications to the collection they are iterating over.

Fail-Fast Iterators

Fail-fast iterators operate directly on the original collection. If the collection is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. This fast failure behavior helps to avoid unpredictable results and potential bugs that might arise from concurrent modification of collections. Examples of fail-fast iterators include those used by ArrayList and HashMap[1][2][3][6].

Fail-Safe Iterators

Fail-safe iterators, on the other hand, work on a clone of the collection, not on the original collection itself. Because they iterate over a separate copy, they do not throw a ConcurrentModificationException if the original collection is modified during iteration. This approach avoids the exception but at the cost of additional memory overhead, as the system must create a copy of the collection. Changes made to the original collection while iterating through it will not be v...

middle

middle

Gợi ý câu hỏi phỏng vấn

junior

Explain what is Binary Search?

expert

What is an efficient way to implement a singleton pattern in Java?

middle

If an object reference is set to null , will the Garbage Collector immediately free the memory held by that object?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào