Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3851920
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:14:14+00:00 2026-05-19T17:14:14+00:00

Implement using existing Synchronized Java classes (Hashtable, StringBuffer, Vector) or synchronize the blocks when

  • 0

Implement using existing Synchronized Java classes (Hashtable, StringBuffer, Vector)

or

synchronize the blocks when we implement Unsynchronized Java classes (HashMap, StringBuilder, ArrayList)

or

create a synchronized collection objects from Collections.synchronizedXXX() method and then use it (of course, i cant synchronize StringBuilder like this.!!)

In a multi-threaded scenario, which of the above will be the best way to achieve synchronization without any performance kill?

Thanks in advance.!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:14:15+00:00Added an answer on May 19, 2026 at 5:14 pm

    The best approach depends on exactly what your code is trying to do and the level of atomicity you require. Sometimes the Collections.synchronizedWhatever is fine; sometimes you need to do your own synchronization.

    In terms of performance you just need to make sure you minimise the number of synchronized blocks that are entered. There will be little difference between

    (example A)
    List l = Collections.synchronizedList(originalList);
    l.add(something);
    

    and

    (example B)
    synchronized (originalList) {
        originalList.add(something);
    }
    

    because they both enter one synchronized block. However:

    (example C)
    List l = Collections.synchronizedList(originalList);
    l.add(something);
    int index = l.indexOf(something);
    

    will enter two synchronized blocks, whereas

    (example D)
    synchronized (originalList) {
        originalList.add(something);
        int index = originalList.indexOf(something);
    }
    

    will only enter one synchronized block. Of course it spends longer in that block so it may increase contention, but the add and the indexOf now behave like a single atomic operation. This may or may not be what you want: it’s entirely application dependent.

    EDIT: To answer Deepak’s question:

    The ‘synchronizedList’ in example C means that each call to a method on ‘l’ will be wrapped inside a synchronized block. You can think of C as doing this:

    synchronized (originalList) {
        originalList.add(something);
    }
    synchronized (originalList) {
        int index = originalList.indexOf(something);
    }
    

    There is some added cost here, but unless it’s in a performance-critical section of your code it’s probably not going to be a problem. I’d suggest you think more about ensuring your code behaves correctly before you think about optimising it. It’s hard to get thread-safe code right so be very careful with how you write things. For example, in C there is a possible race condition between ‘l.add(something)’ and ‘l.indexOf(something)’. D does not have the same race condition because both operations are inside a single synchronized block.

    Brian Goetz’s book (Java Concurrency in Practice) is an excellent resource for learning how to write thread-safe code. I highly recommend it. I’m sure it’s on Amazon.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm at the beginning/middle of a project that we chose to implement using GWT.
It is often useful to implement algorithms using nested array operations. For example, to
I want to implement the solution using the pre-processor described here: Reuse define statement
I am looking to implement horizontal scrolling using jQuery.SerialScroll (based on jQuery.ScrollTo ). I
As part of a larger project I'm trying to implement a facility using JOGL
I'm using managed c++ to implement a method that returns a string. I declare
I am using the jQuery library to implement drag and drop. How do I
Using C# 2.0 what is the best way to implement dynamic form controls? I
I'm using Spring.net with NHiberante (HibernateTemplate) to implement my DAO's. I also have some
How would I implement a binary search using just an array?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.