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

Is this arraylist thread-safe?

Is this arraylist thread-safe?
ArrayList al = (ArrayList)Collections.synchronizedList(new ArrayList());
0
william007
Asked:
william007
2 Solutions
 
avinthmCommented:
Hi ,
When you do Collections.synchronizedList()...you will get an synchronized ArrayList .
which means that it is thread safe.

You can make use of Vector, which is by default thread safe
cheers
0
 
WelkinMazeCommented:
Hi,
Even according to the java api doc it is also thread-save ;)
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#synchronizedList(java.util.List)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I prefer using Vector instead of synchronized array-list because on doing some stress-testing, it was found to give better performance.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Mayank SAssociate Director - Product EngineeringCommented:
You can try that by putting a for loop, adding some 20000 items to the list and then trying to iterate through them. Measure the time difference.
0
 
objectsCommented:
Performance should definitely be better with a VEctor, synchronizedList() uses a sledge hammer approach to making it thread safe by simple synchronising everything whereas Vector is designed as thread safe.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
:-)
0
 
Manikandan ThiagarajanSenior consultantCommented:
0
 
william007Author Commented:
Thanks:-)
0
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.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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