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

senior

How do you ensure that N threads can access N resources without deadlock?

middle

How threadsafe is enum in Java?

entry

What is the difference between JDK and JRE?

Bình luận

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

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