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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:43:13+00:00 2026-06-11T11:43:13+00:00

This is code is copied from internet somewhere: How can this explicit lock work?

  • 0

This is code is copied from internet somewhere:
How can this explicit lock work?

public class Lock {
  private Object lockObj = new Object();
  private Thread owner;
  private int lockCount;

  /**
   * Waits for up to 'maxWait' milliseconds to acquire the lock,
   * and returns true if the lock was acquired.
   **/
  public boolean acquireLock(int maxWait) throws InterruptedException {
    Thread currentThread = Thread.currentThread();
    synchronized (lockObj) {
      if (owner == currentThread) {
        lockCount++;
        return true;
      }
      long waitedSoFar = 0L;
      while (owner != null) {
        long t0 = System.currentTimeMillis();
        long timeToWait = maxWait - waitedSoFar;
        if (timeToWait <= 0)
          return false;
        lockObj.wait(timeToWait);
        if (owner != null) {
          waitedSoFar += System.currentTimeMillis() - t0;
        }
      }
      owner = currentThread;
      lockCount = 1;
      return true;
    }
  }
  public void releaseLock() {
    Thread currentThread = Thread.currentThread();
    synchronized (lockObj) {
      if (owner == currentThread) {
        lockCount--;
        if (lockCount == 0) {
          owner = null;
          lockObj.notify();
        }
        return;
      } else {
        // Only the owner can release the lock!
        throw new IllegalStateException();
      }
    }
  }
}

I haven’t seen any special code to guarantee memory visibility. The only thing related to concurrency is “synchronized(lockObj){ … }”

Is that magic?
Does a CPU just flush all its cache before acquire some synchronization monitor?
Or the opposite?
Does a CPU just flush all its cache when release some synchronization monitor?

EDIT:
Well, I got some other thing related to concurrency wait/notify.

Think about it. How does this explicit lock working?

  1. acquire the lock, modify variable(to prevent other thread to acquire it), then release the lock.
  2. do anything.
  3. acquire the lock,modify variable(to allow other thread to acquire it).
  4. some other thread acquire the lock, and loop…

The happens-before relationship is just between 3 and 4 right?
Or does between 1 and 3 also be guarantee happens-before relationship? So 2 is guaranteed memory visibility?

  • 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-11T11:43:14+00:00Added an answer on June 11, 2026 at 11:43 am

    There is no such thing as a synchronisation monitor that a cpu can acquire. A monitor is a contruct implemented in the runtime of the language you are programming in, in this case, java.

    The java runtime is responsible for this locking and forbidding other code to enter the synchronized block of code. The cpu just sees instructions that deal with it.

    Concerning your question of cache: The cpu doesn’t just decide to flush the cache. The cache stays in place until it is overwritten.

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

Sidebar

Related Questions

I have this sample code for async operations (copied from the interwebs) public class
I have this code copied from Android developers website: public class ExampleActivity extends Activity
I copied this code from the internet and wanted the get a identical result,
I have this code copied from one of questions from SO: public static String
This code is copied from another file and it works in that file, I
I copied this code from an example . I've read it 100 times. Array.prototype.map
I copied the code from this link by giving company name and clicking the
This code is copied directly from http://java.sun.com/docs/books/jni/html/objtypes.html#4013 JNIEXPORT jstring JNICALL Java_Prompt_getLine(JNIEnv *env, jobject obj,
This is the piece of the code I copied from one of the header
I have this code copied from C# 4.0 in a nutshell that uses Attribute/Reflection

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.