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.
LVL 9
william007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.