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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:37:21+00:00 2026-05-22T16:37:21+00:00

once again a question about ArrayList and synchronize. I’d just like to know what

  • 0

once again a question about ArrayList and synchronize.

I’d just like to know what this snippet exactly does:

ArrayList<ObjectX> list = ....;

synchronized (list) {
    if (list.contains(objectxy) == false) {
      list.add(objectxy);
    }
}

Ive got a ArrayList filled with ObjectXs. I want to add then an element to the list, but only if the list doesnt contain the same element. I checked before (in another method) if the list did contain the object – the result was no. But it is possible that two threads at the same time think that the result is no and that they both try then to add objectxy. (there are some other things which must be done inbetween, thats why I cant synchronize the whole process)

So, after the process and when now the threads come to the snippet above, I want to prevent that those two both add the object to the list. So I thought when I synchronize access to the list, only one thread can check if it does contain the object and then add it. After it, the second thread could access the list, see that the object is already in it and would not add it anymore.

Thats what I want to achieve. Would it work? 🙂

So, if yes, Id like to know what the snippet exactly does. Does is prevent two threads from accessing this exact code at the same time? so that the code is only available for one thread at the same time?

Or does it lock the list itself for the whole time, for any thread in the application who is at this moment trying to access the list – anywhere? (I dont have other add()s in my code, but many gets(), thats why Id like to know if other threads can access the list and still get elements while another thread is accessing the code above).

The ArrayList itself is a member-variable which is connected with the principal using the application. It is correct that multiple various threads can access the code above at the same time if they are not sent from the same principal, correct?

So, thats what Id like to know. I tried to mark my questions so that its easier to answer them. Thank you for helping! 🙂

[EDIT] Thank you for all the answers, which almost all said the same! I think it is clear now! 🙂

  • the synchronized code block can be only accessed by one of the principals threads. (threads of other principals are not relevant to the speicific principal). the list itself can be accessed at any time from other threads – as long as the access to it is not synchronized with a synchronize-block too. If it is, the thread must wait until it can access the list (that means, that no other thread is at the same time in a synchronized-block)

correct? I hope so 🙂

  • 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-22T16:37:22+00:00Added an answer on May 22, 2026 at 4:37 pm

    The synchronized block guarantees that only one thread may execute this code block, or any other code block which is synchronized on the same object (i.e. the list) at once. For example, if you have

    synchronized (list) {
        // block A
    }
    
    synchronized (list) {
        // block B
    }
    

    , then if one thread is executing block A, no other thread can be executing block A or block B, because they’re both synchronized on the same object. But the list itself isn’t locked. Another thread might access the list.

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

Sidebar

Related Questions

Once again I've searched for about 45 minutes for an answer to this question
Once again a question about the garbage collector in actionscript-3: If I have a
Once again a very beginner-ish question, but here I go: I would like to
Once again a question about extjs. I use EXT 4.0.2 and have a couple
Once again I have a question about Eclipselink/MOXy with external metadata mapping file. I
Good day, friends. Once again stupid question about Obj-C from newbie :) I'm trying
me again. Sorry about this. Following on from my previous question (I think I
I think I've seen this done once, but I cannot find it again after
This question isn't about the technical usage of restricted, more about the subjective usage.
I feel like this might be a crazy question, and if anyone has a

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.