• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 846
  • Last Modified:

List list=Collections.synchronizedList(new LinkedList());

I have see some code like this

            List list=Collections.synchronizedList(new LinkedList());
            list.add("c");
            list.add("b");
            System.out.println(list.get(0));

Base on my understanding,
the LinkedList is not thread-safe,
meaning that it can be accessed or modified by many threads simultaneously,
so if we use
List list=Collections.synchronizedList(new LinkedList());
it means that the List(Internally using LinkedList) can only be accessed or modified by one thread at one time.

Is my understanding correct? If not, please post a more accurate explanation, thanks.
0
william007
Asked:
william007
3 Solutions
 
rajesh_balaCommented:
Hi William,

Yes. You understanding is correct. If you would prefer to have more cleaner code you could use ConcurrentLinkedQueue in JDK 1.5 (provided you are willing to explore the functionalies in java.util.concurrent package in it)

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html

~Rajesh.B
0
 
objectsCommented:
> meaning that it can be accessed or modified by many threads simultaneously,

ashould be that it can *not* be safely accessed by more than one thread simultaneoulsy.

> it means that the List(Internally using LinkedList) can only be accessed or modified by one thread at one time.

it means that the list can be safely accessed by multiple threads simultaneously.

basically it wraps all methods in a synchronised method
0
 
Manikandan ThiagarajanSenior consultantCommented:
>>List list=Collections.synchronizedList(new LinkedList());

data corruption should be avoided using this one
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mrigankCommented:
basically SynchronisedList is a static inner class present in the Collections class.
It takes a List in its constructor.

It creates a new instance of the SynchronisedList from the list that you pass.
The SynchronisedList is thread safe.
0
 
mrigankCommented:
Collections.synchronizedList(new LinkedList());
 returns you a list but the actual Concrete class is a SunchronisedList as explained above.
0
 
william007Author Commented:
Thanks:-)
0
 
objectsCommented:
no worries :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now