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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:47:58+00:00 2026-05-15T12:47:58+00:00

I know when you want to lock method to be executed by only one

  • 0

I know when you want to lock method to be executed by only one thread you declare it with synchronized keyword.

What about classes, how to provide a lock on an entire class of objects when a thread
is executing some code on an instance of that class?

In other words, when a thread is executing a method on an object, no other thread should be
allowed to execute the same method even on a different instance of the same class.

  • 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-15T12:47:59+00:00Added an answer on May 15, 2026 at 12:47 pm

    You synchronize on a specific object, either some designated static lock object, or the class object (which happens when static methods are declared to be synchronized):

    class X {
        private static final Object lock = new Object();
        public void oneAtATime() {
            synchronized (lock) {
                // Do stuff
            }
        }
    }
    class Y {
        public void oneAtATime() {
            synchronized (Y.class) {
                // Do stuff
            }
        }
    }
    

    Each variant has its own pros and cons; locking on the class allows other code, outside of the class, to use the same lock for its own reasons (which allows it to orchestrate more high-level synchronization than what you provide) while the static final Object lock approach lets you prohibits it by making the lock field private (which makes it easier to reason about the locking and avoid your code from deadlocking because someone else wrote bad code).

    You could of course also use some synchronization mechanism from java.util.concurrent, like explicit Locks, which provide more control over locking (and ReentrantLock currently performs a little better than implicit locks under high contention).


    Edit: Note that static/global locks aren’t a great way to go – it means every instance of the class ever created will essentially be tied to every other instance (which, aside from making it harder to test or read the code, can severely harm scalability). I assume you do this to synchronize some kind of global state? In that case, I’d consider wrapping that global/static state in a class instead, and implement synchronization per-instance rather than globally.

    Instead of something like this:

    class Z {
        private static int state;
        public void oneAtATime(){
            synchronized (Z.class) {
                state++;
            }
        }
    }
    

    Do it like this:

    class State {
        private int value;
        public synchronized void mutate(){ value++; }
    }
    class Z {
        private final State state;
        public Z(State state){
            this.state = state;
        }
        public void oneAtATime(){
            state.mutate();
        }
    }
    // Usage:
    State s1 = new State(), s2 = new State();
    Z foo = new Z(s1);
    Z bar = new Z(s1);
    Z frob = new Z(s2);
    Z quux = new Z(s2);
    

    Now foo and bar are still tied to each other, but they can work independently from frob and quux.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • added an answer Well, start off by thinking of which bits of data… May 17, 2026 at 9:17 am
  • added an answer For this task it is a good idea to use… May 17, 2026 at 9:15 am
  • added an answer This is exactly how the Skyhook database (built into many… May 17, 2026 at 9:15 am

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

Hello i need to use writerreaderlock in my method. I want to know how
I want to know what a virtual base class is and what it means.
I want to know what are the options to do some scripting jobs in
I want to know what exactly is the sequence of calls that occurs when
I want to know which tool can be used to measure the cyclomatic complexity
I want to know how to use variables for objects and function names in
I want to know if i can create a custom google maps application,on which
I want to know how does the SQL Server know what @p# is in
i want to know how to edit a single row (which i select) from
I want to know what is the difference between a query and a view

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.