aspose file tools*
The moose likes Java in General and the fly likes ThreadSafe Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ThreadSafe Collection" Watch "ThreadSafe Collection" New topic
Author

ThreadSafe Collection

subhadeep chakraborty
Ranch Hand

Joined: Jul 29, 2006
Posts: 67
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

Joined: Oct 13, 2005
Posts: 38910
    
  23
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

Joined: Aug 16, 2005
Posts: 14164
    
  19

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.


    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
    Scala Notes - My blog about Scala
    Shanky Sohar
    Ranch Hand

    Joined: Mar 17, 2010
    Posts: 1051

    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.


    SCJP6.0,My blog Ranchers from Delhi
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38910
        
      23
    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

    Joined: Oct 13, 2005
    Posts: 38910
        
      23
    Too difficult a question for "beginning". Moving thread.
    Wouter Oet
    Saloon Keeper

    Joined: Oct 25, 2008
    Posts: 2700

    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:



    "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
    Please correct my English.
    subhadeep chakraborty
    Ranch Hand

    Joined: Jul 29, 2006
    Posts: 67
    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


     
    wood burning stoves
     
    subject: ThreadSafe Collection