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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:00:32+00:00 2026-05-27T09:00:32+00:00

Hi all whenever I use the synchronized statement, I often use this pattern: private

  • 0

Hi all whenever I use the synchronized statement, I often use this pattern:

private static Object lock = new Object();

public void F(){
    //..
    synchronized (lock){
        //..
    }
    //..
}

However, in the source of java.lang.Reference, I see that they employ this pattern instead:

static private class Lock { };
private static Lock lock = new Lock();

public void run() {
//..
synchronized(lock){
    //..
}
//..
}

I was wondering what’s the benefit of declaring a new class Lock (which basically extends Object and do nothing else) ?

Or rather, why didn’t they simply use private static Lock lock = new Object(); ?

  • 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-27T09:00:32+00:00Added an answer on May 27, 2026 at 9:00 am

    The following code:

    synchronized(lock){
    }
    

    doesn’t actually use the Lock mechanics, you’re just using the built-in synchronisation features on an Object. In this case, you might as well use a plain old Object. A benefit of a lock object that extends Object is so that it shows up in debugging tools with the class name rather than just a plain Object, which is more helpful when hunting down deadlocks.

    See here for the Lock API.

    The benefit of Lock is that you get more features, such as being able to ‘try’ locking, then continuing to execute code if that fails. Also, it has different properties than a synchronized block, because it’s not reentrant (a thread can’t hold multiple locks on the same lock, then release them). If you wanted something that was like that, you’d use ReentrantLock.

    You also have cooler locks such as ReentrantReadWriteLock, which support multiple readers, but as soon as a writer locks it, no readers are permitted. There’s a big lock ecosystem in there for different types of application.

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

Sidebar

Related Questions

It seems like whenever I use the Command Pattern, it always leads to a
I created a uiview(with two buttons) covers all the screen whenever i click on
I'm handling all of my unhanded exception in the code but whenever one happens
Whenever i perform a file search in eclipse, it scans all files including .svn-base
I am making a toast in vb.net, and whenever it pops up, all the
Whenever we use chmod, we set different authorities to different users types like owner,
I'm trying to use NHibernate in a project. All my domain tables include a
I have a FULLTEXT index called cart_product_fti on a table Whenever I use it,
I have encountered a very strange issue. Whenever I use the .NET membership provider
I am using hg version 1.9.1. Problem is whenever I use hg update, I

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.