multithreading - Thread-safe Enumset in Java -
multithreading - Thread-safe Enumset in Java -
i utilize next code initialize synchronized instance of enumset:
private final set<myclass> instance = collections.synchronizedset(enumset.noneof(myclass.class));
i have 2 questions:
do retain benefits of enumset compactness , efficiency in case? is there more... let'say... semantically rich way empty , synchronized instance of enumset?
well javadoc:
if multiple threads access enum set concurrently, , @ to the lowest degree 1 of threads modifies set, should synchronized externally. typically accomplished synchronizing on object naturally encapsulates enum set. if no such object exists, set should "wrapped" using collections.synchronizedset(java.util.set) method. best done @ creation time, prevent accidental unsynchronized access:
set s = collections.synchronizedset(enumset.noneof(myenum.class));
so think that's best can do.
i maintain set
final
did. it's odd don't mention in javadoc.
edit: reply first question, short reply yes, long answer, yes have pay cost synchronization on top of that.
java multithreading enumset
Comments
Post a Comment