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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:41:11+00:00 2026-05-15T07:41:11+00:00

If I have 2 synchronized methods in the same class, but each accessing different

  • 0

If I have 2 synchronized methods in the same class, but each accessing different variables, can 2 threads access those 2 methods at the same time? Does the lock occur on the object, or does it get as specific as the variables inside the synchronized method?

Example:

class X {

    private int a;
    private int b;

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

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

}

Can 2 threads access the same instance of class X performing x.addA() and x.addB() at the same time?

  • 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-15T07:41:12+00:00Added an answer on May 15, 2026 at 7:41 am

    If you declare the method as synchronized (as you’re doing by typing public synchronized void addA()) you synchronize on the whole object, so two thread accessing a different variable from this same object would block each other anyway.

    If you want to synchronize only on one variable at a time, so two threads won’t block each other while accessing different variables, you have synchronize on them separately in synchronized () blocks. If a and b were object references you would use:

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

    But since they’re primitives you can’t do this.

    I would suggest you to use AtomicInteger instead:

    import java.util.concurrent.atomic.AtomicInteger;
    
    class X {
    
        AtomicInteger a;
        AtomicInteger b;
    
        public void addA(){
            a.incrementAndGet();
        }
    
        public void addB(){ 
            b.incrementAndGet();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 498k
  • Answers 498k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Change your GetSystems method to GetSystems(String filterword) and provide the… May 16, 2026 at 12:10 pm
  • Editorial Team
    Editorial Team added an answer All hooks in Drupal are fired in module-weight order. By… May 16, 2026 at 12:10 pm
  • Editorial Team
    Editorial Team added an answer I couldn't find a good one. I found a flexible… May 16, 2026 at 12:10 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is using the 'synchronized' keyword on methods in a Java DAO going to cause
I have a class called communicator. This class is a listener of a thread
I know the difference between synchronized method and synchronized block but I am not
This is my first time using the synchronized keyword, so I am still unsure
I have this situation: web application with cca 200 concurent requests (Threads) are in
We have a decent set of unit tests on our code and those unit
I have a Network Client class that is receiving a large binary block and
I just need a confirmation that i have understood the concept of locks in
today I have coded a test case for my application, to see how transactions
I am having this doubt about multi-threading and I have faced lots of questions

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.