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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:15:33+00:00 2026-05-15T10:15:33+00:00

I have a confusion about object lock. The below class having 4 methods, the

  • 0

I have a confusion about object lock.
The below class having 4 methods, the method addB() is synchronized.

In my scienario, there are 4 threads. When a thread-2 access the addB() method (it creates a lock on Test object), will there any other thread access addC() or addD() same time?

Does the Object lock allows only one thread at a time ?

class Test{
       private Integer a;
       private Integer b;
       private Integer c;
       private Integer d;


   public void addA(){
      synchronized(a) {
         a++;
      }
   }
   public synchronized void addB(){
         b++;
      }

   public void addC(){
         c++;
      }

   public void addD(){
         d++;
      }    
   }

EDIT:
I have 3 threads(t1, t2 and t3) , and each one is going to access addB(), addC() and addD(). If thread t1 access the method addB(), could thread t2 access addC() method simultaneously? If not what would be t2 state?

class Test{
       private Integer a;
       private Integer b;
       private Integer c;
       private Integer d;


   public void addA(){
      synchronized(a) {
         a++;
      }
   }
   public synchronized void addB(){
         b++;
      }

   public synchronized void addC(){
         c++;
      }

   public synchronized void addD(){
         d++;
      }    
   }
  • 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-15T10:15:34+00:00Added an answer on May 15, 2026 at 10:15 am

    A lock does indeed allow only one thread at a time, but different locks do not affect each other.

    In your example, you have two locks – one on the mutex belonging to a, and one on the mutex belonging to this (which is implicit when you use the synchronized keyword, as you correctly mentioned in your post).

    So calls to addB() will be synchronized but will not block calls to any other method. If one thread holds the lock on this, another thread can hold the lock on a, and multiple other threads can execute addC() and addD() concurrently.

    Edit: as an aside, you might be interested to learn about the AtomicInteger class if you really are working with Integers. They provide atomic operations such that you don’t need to worry about synchronizing around them.

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

Sidebar

Related Questions

i have few confusion regarding ORM(Object Relational Mapping) i am thinking about to use
I am creating a linklist class and have some confusion about reference to objects.
I have a little confusion about accessing private inner classes of an object with
I have confusion about what exactly people mean by Object Literals, JSON, JavaScript Objects,
One thing I have continually found very confusing about using an object database like
I have some confusion about when to use the self keyword during ivars assignment,
Here, I have a bit confusion about UI language. If language is changed then
I think I may have some confusion about the domain/service layer separation. In my
I have confusion about the concept of inheritance in C++, suppose we have a
I am having a confusion regarding object modeling, which I will illustrate using 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.