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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:16:55+00:00 2026-06-18T01:16:55+00:00

If a synchronized block of code contains an unsynchronized collection. Is the collection considered

  • 0

If a synchronized block of code contains an unsynchronized collection. Is the collection considered thread safe? If not, can you provide any practical scenarios where two threads could unsafely access the collection within the synced code?

Thanks.

  • 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-18T01:16:56+00:00Added an answer on June 18, 2026 at 1:16 am

    Only if ALL the code that access the collection is synchronized and they use the same “object” to synchronize it.

    For example, the code below would not be synchronized because they are synced to different objects.

    public class Foo {
        private final Collection<object> collection;
    
        public void Foo(Collection<object> collection) {
           this.collection = collection;
        }
    
        public synchronized add(Object o) {
           this.collection.add(o);
        }
    }
    
    public class Bar {
        private final Collection<object> collection;
    
        public void Bar(Collection<object> collection) {
           this.collection = collection;
        }
    
        public synchronized print() {
           for (Object o : collection) { System.out.println(o); }
        }
    }
    

    Then you could have a situation that you expected a Object o to be printed because you thought that it was added before, but the thread that was doing so was halted before the add was finished.

    It’s easier to imagine this like if you have someone that has a flag to indicate that you can access some place. If the flag is high up, you can not enter the block. This person is always created when you create an Class instance and it’s bound to it. So, in the code bellow we would have three “flag person”.

    ...
    Collection<Object> objs = new ArrayList<Object>();
    Foo foo = new Foo(objs);
    Bar bar = new Bar(objs);
    ...
    

    the synchronized statement indicates the flag person to raise its flag after someone passes through it and put it down when it exists the block. Because we set the synchronized to the class method, it’s bound to the “flag person” of that instance. So, diffent “flag persons” would raise their hands to someone enter the block where the collection is handled, but because they both are not synchronized to each other, they would let anyone enter, even if the other has its flags raised.

    To solve this, you need only one person to handle the flags. To do this, you need a shared flag person. In this case, you could use the collection itself. So, you would have something like

    public class Foo {
        private final Collection<object> collection;
    
        public void Foo(Collection<object> collection) {
           this.collection = collection;
        }
    
        public synchronized add(Object o) {
           synchronized (collection) {
               this.collection.add(o);
           }
        }
    }
    
    public class Bar {
        private final Collection<object> collection;
    
        public void Bar(Collection<object> collection) {
           this.collection = collection;
        }
    
        public print() {
           synchronized (collection) {
               for (Object o : collection) { System.out.println(o); }
           }
        }
    }
    

    Because only collection “flag person” is raising its flag, everyone will access the collection accordingly to “who comes first” and not “who finishes first”.

    I think I made my explanation a little more difficult than it should hehe but I hope it can helps. If I could draw here, it would probably be better understood 😛

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

Sidebar

Related Questions

I think I can use any object to be synchronized as block such as:
Is it possible to pass over a synchronized block of code when another thread
Is there any way to block a critical area like with Java synchronized in
I know the difference between synchronized method and synchronized block but I am not
I was trying to cut thread contention in my code by replacing some synchronized
Refer below code public void acquire(){ synchronized(a){ print(acquire()); try{ //Thread.sleep(5000); synchronized(this){ wait(5000); } print(I
As i understand the code below, in the synchronized block, this is an instance
I once saw a snip of code as below, /** Starts a synchronized block
In the below code listings , are Statement 1 and Statement 2 thread safe
I have read that code inside a synchronized block conform to 'happens before' semantics

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.