Apparently, there are two ways to obtain a thread-safe HashSet instance using Java’s Collections utility class.
I ask:
- How do they differ?
- Which, and under what circumstances, is to be preferred over the other?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What you may be thinking of is
This supports concurrent updates and reads. Its Iterator won’t throw ConcurrentModicationException. where as
Is more light weight but only allows one thread at a time to access the set. You need to lock the set explicitly if you want to Iterator over it and you can still get a CME if you don’t update it in a safe way (while iterating over it)