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

  • Home
  • SEARCH
  • 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 926931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:46:12+00:00 2026-05-15T19:46:12+00:00

I often find myself with code like private static final MyType sharedResource = MyType();

  • 0

I often find myself with code like

private static final MyType sharedResource = MyType();
private static final Object lock = new Object();
...
synchronized(lock)
{
    //do stuff with sharedResource
}

Is this really neccessary or could sharedResource be used as the lock itself like

private static final MyType sharedResource = MyType();
...
synchronized(sharedResource)
{
    //do stuff with sharedResource
}

Note: synchronized blocks shown in the examples would live within methods doing work and are not methods themselves or synchronized methods.

EDIT: A very good point has been pointed out in some of the answers that if we are dealing with multiple shared resources that the first “Object” technique is far safer.

WARNING The fact that sharedResource is static is important! If it is static then synchronized methods or synchronized blocks locking on this won’t work. The lock object must also be static.

  • 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-15T19:46:13+00:00Added an answer on May 15, 2026 at 7:46 pm

    Pros and cons of both

    First option pros: Allows locking on concept, not on Objects. If you had to lock on multiple resources for a single action (which is usually not advised, but sometimes necessary) you could do this with much less worry about race conditions.

    cons: The object could still be modified, so you need to ensure access to the object is restricted to methods that abide by the external lock.

    Second option pros: Lock on the object should prevent others from modifying it (although you should double check the exact symantics.) EDIT: Has the same con as above – if the methods aren’t synchronized internally, you could still run into methods that don’t abide by the contract.

    cons: You block access to all methods, even those unrelated to what you’re trying to operate, which could cause slowdowns and possibly deadlock. You could easily make the case, however, that if that’s the case your Object is doing too much work and should be broken up.

    EDIT: Allow me to clarify part 2 here (the case to break up MyType into MyFoo and MyBar is open for debate…)

    class MyType {
       Foo foo;
       Bar bar;
    
       void doFoo() { foo.do(); }
       void doBar() { bar.do(); }
    }
    
    class MyActions {
        MyType thing;
    
        void lotsOfFoo() {
            // blocks bar :-(
            synchronized(thing) { thing.doFoo(); } 
        }
    
        void lotsOfBar() {
            // blocks foo :-(
            synchronized(thing) { thing.doBar(); } 
        }
    
    }
    

    Personally, I use option 1 much more often (that’s why I’m unsure about that part in option 2).

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

Sidebar

Related Questions

Using ValueInjecter, I often find myself writing code like this: var foo1 = new
Often I find myself coming across code like this: try { StreamWriter strw =
Often I find myself writing code like this: if (Session != null) { Session.KillAllProcesses();
Quite often I find myself writing code like this: Dim oRenewalOrder = (From c
While striving for const-correctness, I often find myself writing code such as this class
My PHP code is split between many files, and often I find myself using
I find myself using the current pattern quite often in my code nowadays var
I often find myself needing reference to an object that is several objects away,
I find myself very often creating an object that has no public methods and
I often find myself in a quandary in where to put serialisation code for

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.