?
Solved

Is this arraylist thread-safe?

Posted on 2006-04-17
8
Medium Priority
?
1,336 Views
Last Modified: 2012-08-13
Is this arraylist thread-safe?
ArrayList al = (ArrayList)Collections.synchronizedList(new ArrayList());
0
Comment
Question by:william007
8 Comments
 
LVL 6

Accepted Solution

by:
avinthm earned 1600 total points
ID: 16467459
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
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16467503
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
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 400 total points
ID: 16467529
I prefer using Vector instead of synchronized array-list because on doing some stress-testing, it was found to give better performance.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16467655
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
 
LVL 92

Expert Comment

by:objects
ID: 16467790
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16467807
:-)
0
 
LVL 9

Expert Comment

by:Manikandan Thiagarajan
ID: 16470138
0
 
LVL 9

Author Comment

by:william007
ID: 16478696
Thanks:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question