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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:12:47+00:00 2026-06-07T05:12:47+00:00

As i understand the code below, in the synchronized block, this is an instance

  • 0

As i understand the code below, in the synchronized block, this is an instance of a counter.

Question 1: In the example below, does this mean that when Thread A reaches the synchronized block, Thread B is blocked from doing anything to the instance of a Counter?. In other words, does this mean that Threads may continue to execute as they see please, but upon either reaches the synchronized block, the other one is stopped from doing anything to the class until the block exited?

public class Counter {

    public void increment() {
        // Some code

        synchronized (this) {  // <---- "this" is an instance of Counter
             // Some more code
        }
    }
}

Compare code above with

public class Counter {

    List<String> listOfStrings = new ArrayList<String>();

    public void increment() {
        // Some code

        synchronized (listOfStrings) {  
             // Some code that deals with 
             //    listOfStrings 
        }
    }
}

Question 2: In the example above, once Thread A reaches the synchronized block, Thread B may continue reading and writing anything in the class with the exception of listOfStrings ArrayList, which is a mutex in the synchronized block. Is this correct?

Question 3: Is it further correct to assume that if we need to make modifications to multiple objects, this is the mutex we should use?

For example:

public class Counter {

    List<String> listOfStrings = new ArrayList<String>();
    List<Integers> listOfIntegers = new ArrayList<Integers>();

    public void increment() {
        // Some code

        synchronized (this) {  
             // Some code that deals with 
             //    listOfStrings and listOfIntegers
        }
    }
}

Am i understanding things correctly? Please correct if i misstated anything.

  • 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-07T05:12:49+00:00Added an answer on June 7, 2026 at 5:12 am

    Thread B is blocked from doing anything to the instance of a Counter?

    No, Thread B is blocked from entering the synchronized blocks of code, it can still enter other methods: those that are not synchronized and those synchronized using different objects. Thread B cannot only access blocks synchronized using an object already taken by a different thread (synchronized locks are re-entrant).

    Thread B may continue reading and writing anything in the class with the exception of listOfStrings

    Not really, that fact that listOfStrings is used as a mutex in one synchronized block does not mean other threads cannot access that object explicitly. It only means that other thread cannot access synchronized blocks guarded by the same object. So if you want to secure access to listOfStrings object, all methods accessing that object must be synchronized and use the same lock (e.g. listOfStrings).

    BTW every object you synchronize on should be final to avoid headaches.

    Is it further correct to assume that if we need to make modifications to multiple objects, this is the mutex we should use?

    Yes and no. Consider the following case:

    List<String> listOfStrings = new ArrayList<String>();
    List<Integers> listOfIntegers = new ArrayList<Integers>();
    Set<String> setOfStrings = new HashSet<String>();
    Set<Integers> setOfIntegers = new HashSet<Integers>();
    

    If one method only accesses lists and the second method only accesses sets, you can safely use two locks – one for the first method and second one for the second method. Synchronizing on this won’t hurt, but it will impact performance:

    private final Object listLock = new Object();
    private final Object setLock = new Object();
    

    and later:

    synchronized (listLock) {  
         // Some code that deals with 
         // setOfStrings and setOfIntegers
    }
    
    //...
    
    synchronized (setLock) {  
         // Some code that deals with 
         // setOfStrings and setOfIntegers
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

it's a pretty basic question but I don't understand why the code below does
I understand that the code below would result segmentation fault because at the cstr
I cannot understand why the code below is giving me this error in firebug
Consider the code below. I don't understand why my GCC compiler does not try
There is something that I really don't understand with the HttpListener. The code below
New programmer here, I am trying to understand and break down this code below
I can't understand what the below ruby code does. Can anyone give me some
In the below code I understand that sys.argv uses lists, however I am not
i understand that the code given below will not be compltely understood unless i
Please help me to understand below code. This is the script for drag and

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.