• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ThreadSafe Collection

 
subhadeep chakraborty
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I need an ThreadSafe collection.I am unable to understand the difference between unmodifiablexxx() and Singletonxx() of Collections class ,both seem to return immutable objects. So are they threadSafe ?

If I can use immutable object I donot want to use synchronized Collections (synchronizedxxx())
Please Advice.

Thanks,
Subhadeep
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The unmodifiable lists are not immutable; the unmodifiable bit is slightly misleading. If the original List is changed, the copy is changed. Regard it as read-only rather than immutable.
I have never tried singletonList().
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The lists that Collections.unmodifiableList() and Collections.singletonList() return are two completely different things.

  • unmodifiableList returns a wrapper around the original list that you put in, which provides a read-only view on the underlying list.
  • singletonList creates a new, read-only list with a single element in it.


  • The obvious difference is that singletonList returns a list with only one element in it. The list that singletonList returns is optimized; it always has one element, so methods such as size() can be implemented very simply with return 1;, for example.

    But unmodifiableList and singletonList both don't have anything to do with synchronized collections.

    By default, collection classes such as ArrayList and HashMap do not have any synchronization (because you rarely need this, and it does add a performance overhead). If you really need a synchronized list, for example, you can wrap it using Collections.synchronizedList():

    If your collection is immutable, you do not need synchronization, because no threads will be able to modify it.

    What Campbell says is true; Collections.unmodifiableList() is only a view on an existing list, you could still change the existing list, and you'll see the changes also in the view that unmodifiableList returned. However, if you throw away any references to the original list (and only keep the wrapped list that unmodifiableList returned), then for all practical purposes your list will be immutable.

     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    subhadeep chakraborty wrote:
    I need an ThreadSafe collection.


    Please use the one that are already available like "Vector" or for Map "HashTable".

    Because if you make the Collection like List,ArrayList etc to Threadsafe by using methods of Collections class,then still its not been guranteed that the methods will work in a synchronized way.

    Can anybody comment if i am wrong.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50289
    80
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    shanky sohar wrote:
    subhadeep chakraborty wrote:
    I need an ThreadSafe collection.


    Please use the one that are already available like "Vector" or for Map "HashTable".

    Because if you make the Collection like List,ArrayList etc to Threadsafe by using methods of Collections class,then still its not been guranteed that the methods will work in a synchronized way.

    Can anybody comment if i am wrong.
    Not convinced. You need to create the safe collections before they are used by anything, however.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50289
    80
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Too difficult a question for "beginning". Moving thread.
     
    Wouter Oet
    Saloon Keeper
    Posts: 2700
    IntelliJ IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You could also use the classes in the *new* java.util.concurrent.* package. With those classes there is no need for external synchronization because the classes contain useful atomic operations which would otherwise require external synchronization. An example:

     
    subhadeep chakraborty
    Ranch Hand
    Posts: 67
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,
    I am really sorry for this unavoidable delay. Please don't think otherwise.
    I have a small doubt.
    if(result != null) // Case another thread altered it
    System.out.println("Previous value: " + result);
    }


    As I understand, this is to understand when element has been added.

    and,
    But the above is about 250% faster

    This is because the above code will call putifAbsent() less frequently.

    Is my understanding correct ?

    Thanks,
    Subhadeep


     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic