java - Concurrency on Treeset using last method -


I want to apply a bean where I have TreeSet in the sorted order Caching Integer Only this method which uses TreeSet is addValue which is adding a new integer to the set, and getHighestValue using the sorted set from the previous () method is returning the final value in it.

Is there any concurrency issue here? I am not using a clear Iterator, so there should be no concurrent problem in getting the highest value, but I do not know the last method can throw any of concurrent amendment or any other Exceptions if try two threads to add and receive the highest value at the same time.

Yes, assuming multiple thread sets, and at least one is modified, there are concurrency worries By the way, you add many threads to action, which are definitely causing problems - worse than that, they ConcurrentModificationException s

ConcurrentModificationException You have to be careful about issues of incorrect concurrency, such as trying to delete an item from a collection, as you repeat it ( It is not clear what the collection should do, in such a case). However the exception can be raised when the collection becomes aware of the wrong revision. Since this collection is not thread-safe, it does not explicitly guarantee multi-threaded operation, and you are expected to protect yourself from the collection explicitly.

The easiest, though least efficient, way to do this is to wrap it before using it, that is:

  sortedSet synchronizeset = collection. Trendedetset (new tree - set);  

This ensures that each method will be done in the call serial, which means that they are waiting to complete any pre-call. However, in this way, you essentially lose most of the benefits of multi-threading.

Another option is to use a clearly thread-protected sorted code , that is:

This implementation requires the expected average logs (N) contains , addition , and for time costs> operations and their variants inclusion, removal , And access operations performed concurrently by multiple threads.

This implementation allows you to interact with sets with multiple threads without further worries. It is not to say that this is the best method to implement the behavior you have seen, but given what you have described - adding and accessing the maximum value in a set of sets with multiple threads - This is what you are looking for.

Also see:


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -