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...
2 hours ago
-
Top 5 Online Courses to Learn MongoDB NoSQL Database in 2025 - Best of Lot
-
MongoDB is one of the leading NoSQL databases and it was on my radar for a
long time but I never get a chance to learn or work on it, but Recently I
got a...
14 hours 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...
5 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...
7 months ago
-
Book Review: The Girl in White Gloves by Kerri Maher
-
[image: The Girl in White Gloves]*The Girl in White Gloves by Kerri Maher*
*Goodreads*
I was curious to learn about old Hollywood, but beyond that I wasn't ...
4 years ago
-
convert .pem file to .ppk using puttygen - AWS
-
PEM-Privacy Enhanced Mail is a Base64 encoded DER certificate file
format. PEM certificates are frequently used for web servers as they can
easily be tra...
4 years ago
-
Spring Boot Webflux DynamoDB Tutorial
-
Creating REST API in Spring Boot Webflux and AWS DynamoDB. Step by step
guide for storing retriving data in DynamoDB using Spring Boot Webflux and
exposing...
5 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
-
What are the topics to focus on AWS Certified SysOps Administrator
Associate certification exam preparation?
-
AWS Certified SysOps Administrator Associate exam is comparatively
toughest exam in the associate level exams. Other two exams Solutions
Architect and Dev...
7 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
-
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
-
Wildfly 8.x : Control maximum number of connections (threads) assigned to
an application
-
In Wildfly, multiple applications can be deployed together. With the
default configuration, there is no control on the maximum number of
connections/threa...
9 years ago
-
POI Hide UnHide Rows Columns Java Example
-
In this tutorial, we will discuss how to hide / unhide rows / columns in an
Excel worksheet using Apache POI, with suitable Java Examples.We will cover
the...
10 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...
15 years ago
-
Newest Blogger Templates
-
Click for more templates below:
Favorite templates | Previous templates 161 - 170 >>
15 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.