Is it possible to synchronized ArrayList?
ArrayList is not synchronized. But there's a way the get a synchronized
one as mentioned in java.util.ArrayList's JavaDoc:
List list = Collections.synchronizedList(new ArrayList(...));
In java.util.Collections' JavaDoc you can read that "It is imperative
that the user manually synchronize on the returned list when iterating
over it:"
synchronized(list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
Using a synchronized ArrayList results in additional work, why not use a
java.util.Vector instead? Is there an advantage in using this approach?
I'm not sure which extra work you're talking about. Do you mean the fact
that you have to call Collections.synchronizedList() with ArrayList, or do
you mean the above example of synchronizing on iteration? For the latter,
the same would hold true for Vector. If you want to iterate over a Vector
which may be being modified from other threads, you would need to put the
whole iteration in a synchronized block, just as you would need to for
ArrayList. Vector just has synchronized methods, but it can't automatically
synchronize across multiple method calls any more than a synchronized
ArrayList can.
As for using
Collections.synchronizedList(new ArrayList())
vs.
new Vector()
It's really just because the Collections hierarchy is a newer addition to
Java than Vector, and is meant to replace Vector. Vector is just kept
around because there was too much code already using it by the time
Collections were added to the standard library. One of the design decisions
made with the new Collections classes was to make them not be synchronized
by default, and instead provide a way to optionally make them be
synchronized.
I believe the reason for this is that most of the time, method-level
synchronization isn't needed for Collections (or Vectors). Either the
Collection is only accessed from one thread, or any code that's accessing or
modifying it will be contained in some other synchronized method or block as
part of some larger synchronized operation. So making Collections default
to having synchronized methods would just provide a false sense of security
to some programmers, while providing new opportunities for deadlocks.
My Favorite Site's List
-
Build Your Own Test Framework
-
[image: Build Your Own Test Framework]
Learn to write better automated tests that will dramatically increase your
productivity and have fun while doing so...
38 minutes ago
-
How to enable/disable form elements using jQuery? Examples
-
In last couple of tutorials you have learned how to get all selected
checkbox and radio buttons, and in this tutorial, you will learn how to
enable/disab...
4 days ago
-
EndOfLife Software packages
-
The "End of Life" (EOL) page for Software provides information on the
release dates, support periods, and security status of different Software
versions. I...
4 months ago
-
How to Install Java on Ubuntu 24.04
-
Connect to us ( @twitter | @facebook )
Java is a versatile programming language that can be installed on Ubuntu
24.04 using different methods, such as “a...
6 months ago
-
Demystifying the Top 10 Spring Annotations for 2024
-
Introduction:
Spring annotations are a powerful tool for autoconfiguring Spring and
streamlining the wiring up of components. They serve as metadata that
...
11 months ago
-
Vedic Maths Classes Online - Sydney
-
If you are keen to learn Vedic Maths, and have full commitment and passion
to do so, please reach out to me at this email address for online classes
- stud...
2 years ago
-
Mini-Review Roundup
-
*Mini-Review Roundup*
[image: Marie Antoinette: The Journey]
*Marie Antoinette: The Journey by Antonia Fraser*
Honestly, this was kind of a letdown...
3 years ago
-
Java URL Encoder/Decoder Example
-
Java URL Encoder/Decoder Example – In this tutorial we will see how to URL
encode/decode attributes in Java. Overview URL encoding, is a mechanism
for enco...
4 years ago
-
Top Developer Collaboration Tools
-
How to drive your project into a corner? Just in case you wondered, there
are multiple options. The surest one is miscommunication. Considering that
you ca...
5 years ago
-
Highlighting Google Cloud’s talks at Puppetize Live
-
Highlighting Google Cloud’s talks at Puppetize Live
Kristina Psaris-Weis 16 October 2018
Off
[image: Andrew Nhem]
Andrew Nhem
Events — Partn...
6 years ago
-
Meet The Wattpad Stars: Brittany Geragotelis
-
After 10 years of pounding the pavement with publishers and agents,
Brittany Geragotelis decided to break free from gatekeepers by posting an
original stor...
7 years ago
-
My Notes On Time Series
-
Time Series
Data set for which time lies in one of the axis is known as time series.
Most of the economical data, meteorological data is an example of time...
8 years ago
-
Breaking Down Barriers in Sexual and Reproductive Health Reporting in Africa
-
*This is a guest post by Humphrey Nabimanya, founder of Reach a Hand
Uganda. *
[image: 2016-04-15-1460736651-1435623-huffpo1.jpg]*Journalists and bloggers...
8 years ago
-
OSGi - Road Ahead
-
OSGi has been introduced many times in the past few years and will still
require some in the future1. With so many introductions, one might be
inclined to ...
11 years ago
-
The Two Ways of Doing a Job
-
Whether it's deployment, development, performance tuning, troubleshooting
or something else, there are two fundamentally different ways of doing your
job: ...
12 years ago
-
List of demonstrations on this page And How To Find Them And How To Get
Them.
-
Here Is A List of demonstrations on this page with the link back to the
tutorial :
How To Add A Scrolling Recent Posts Gadget To Your Blog
You Can See T...
14 years ago
-
Newest Blogger Templates
-
Click for more templates below:
Favorite templates | Previous templates 161 - 170 >>
14 years ago
-
Blog Members
-
Here is how to show off your blog members. These are members of your blog
community who have joined your blog through Google Friend Connect. Put all
your m...
15 years ago
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
#update below script more than 500 posts
No comments:
Post a Comment
I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.