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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:24:07+00:00 2026-05-25T17:24:07+00:00

While reading about synchronization, I came across monitor pattern to encapsulate mutable states. The

  • 0

While reading about synchronization, I came across “monitor pattern” to encapsulate mutable states.

The following is the sample code

   public class MonitorLock {
      private final Object myLock = new Object();
      Widget widget;
      void someMethod() {
        synchronized(myLock) {
         // Access or modify the state of widget
        }
    }

}

Is it better in any way to have a private lock instead of the intrinsic lock?

  • 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-25T17:24:08+00:00Added an answer on May 25, 2026 at 5:24 pm

    Yes – it means you can see all the code which could possibly acquire that lock (leaving aside the possibility of reflection).

    If you lock on this (which is what I assume you’re referring to by “the intrinsic lock”) then other code can do:

    MonitorLock foo = new MonitorLock();
    synchronized(foo) {
        // Do some stuff
    }
    

    This code may be a long way away from MonitorLock itself, and may call other methods which in turn take out monitors. It’s easy to get into deadlock territory here, because you can’t easily see what’s going to acquire which locks.

    With a “private” lock, you can easily see every piece of code which acquires that lock, because it’s all within MonitorLock. It’s therefore easier to reason about that lock.

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

Sidebar

Related Questions

I came across the following while reading about how easy it is to redefine
While reading some articles about writing web servers using Twisted , I came across
I am confused about StringPool in Java. I came across this while reading the
While reading about shell scripts and temporary file handling, I came across Symlink Exploits.
Came across the term pigeon computing while reading about how google works.. Can someone
I came across below text while reading about Database Connection pool properties: The maximum
While reading Learning Perl , I came across a problem about regular expressions. In
While reading about Google webtool kit, came across a statement saying 'synchronous RPCs are
I came across this question, while reading about std::array and std::vector .
While reading about exception, I will always come across checked exceptions and unchecked exceptions,

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.