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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:20:14+00:00 2026-06-17T22:20:14+00:00

For example I have a class with 2 counters (in multi-threaded environment): public class

  • 0

For example I have a class with 2 counters (in multi-threaded environment):

public class MyClass {
  private int counter1;
  private int counter2;

  public synchronized void increment1() {
        counter1++;
  }

  public synchronized void increment2() {
        counter2++;
  }

}

Theres 2 increment operations not related with each other. But I use same object for lock (this).

It is true that if clients simultaneously calls increment1() and increment2() methods, then increment2 invocation will be blocked until increment1() releases the this monitor?

If it’s true, does it mean that I need to provide different monitor locks for each operation (for performance reasons)?

  • 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-17T22:20:16+00:00Added an answer on June 17, 2026 at 10:20 pm

    It is true that if clients simultaneously calls increment1() and increment2() methods, then increment2 invocation will be blocked until increment1() releases the this monitor?

    If they’re called on the same instance, then yes.

    If it’s true, does it mean that I need to provide different monitor locks for each operation (for performance reasons)?

    Only you can know that. We don’t know your performance requirements. Is this actually a problem in your real code? Are your real operations long-lasting? Do they occur very frequently? Have you performed any diagnostics to estimate the impact of this? Have you profiled your application to find out how much time is being spent waiting for the monitor at all, let alone when it’s unnecessary?

    I would actually suggest not synchronizing on this for entirely different reasons. It’s already hard enough to reason about threading when you do control everything – but when you don’t know everything which can acquire a monitor, you’re on a hiding to nothing. When you synchronize on this, it means that any other code which has a reference to your object can also synchronize on the same monitor. For example, a client could use:

    synchronized (myClass) {
        // Do something entirely different
    }
    

    This can lead to deadlocks, performance issues, all kinds of things.

    If you use a private final field in your class instead, with an object created just to be a monitor, then you know that the only code acquiring that monitor will be your code.

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

Sidebar

Related Questions

Example-I have a person class Public Class Person Private _fname As String Public Property
public class counting { private static int counter = 0; public void boolean counterCheck(){
For example can I have class Dog { int legs; Bone chewy; Bone squeeky;
Example I have a repository class (DAL): public class MyRepository : IMyRepository { public
I have an example class containing two data points: public enum Sort { First,
For example is it better to have: public class Case : EntityIdentifiable { public
Let's say I have this program: class Foo { public: unsigned int bar ()
I have the following example classes in Java: public class A { } public
I have the following class: public class Vertex() { private double xCoord; private double
Example: I have an class that inherits from UIImageView. An object creates an instance

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.