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 8208557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:24:52+00:00 2026-06-07T09:24:52+00:00

If I have a class, call it X and X contains a collection (assume

  • 0

If I have a class, call it X and X contains a collection (assume I am not using one of the synchronized colections, just a normal one).

If I was to write my own method synchronized add()- how does the locking work? Is the locking done on the instance of X, and not on the collection object?

So synchronizing my add() method would not stop many instances of X from calling add() and inserting into the collection- therefore I could still have threading problems?

  • 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-06-07T09:24:53+00:00Added an answer on June 7, 2026 at 9:24 am

    A synchronized method locks the object. If your X.add is synchronized, it will prevent concurrent execution of other synchronized methods of the same X object. If anyone out of that X object has access to the same collection, the collection will not be protected.

    If you want your collection to be protected, make sure it is not accessible to the rest of the world in any way other than a synchronized method of X. Also, this is a bit unclear in your question, but note that a synchronized non-static method locks the object. Assuming each X instance will have a collection of its own, they won’t interfere with each other.

    Another option, BTW, is to lock the collection instead of the X object:

    void add(Object o) {
       synchronized(myCollection) {
          myCollection.add(o);
       }
    }
    

    This will synchronize access to the locked collection instead of the X object. Use whichever you find easier and more effective.

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

Sidebar

Related Questions

I have this class file call SMSHelper.cs First I just wanted to know is
I have a Course class which contains collection of CourseItems . Each CourseItem has
I have a Class(call it main class) and the method in main class calls
I have small class called 'Call' and I need to store these calls into
i have a class that uses a proxy class inside to call a service
In Visual Studio 2008 (C++) I have a class destructor (Let's call this class
I have a class annotated with @Configuration (let's call it StubConfiguration ) which has
I have a superclass we can call class A and few subclasses, e.g. class
In my extended PreferenceActivity class I call addPreferencesFromResource(R.xml.livewallpaper_settings) so I have a xml file
I have a class that requires dynamically setting a List. I also call this

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.