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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:44:17+00:00 2026-06-02T04:44:17+00:00

Let’s say I have a code snippet like this synchronized(obj) { do something; }

  • 0

Let’s say I have a code snippet like this

synchronized(obj) {
    do something;
}

If obj was already locked by some other thread, this code will wait until obj released and after that it will try to get the lock.

But I want to know if there is any way to jump over the code block if the lock can not be gained immediately?

Or put it another way, is there a way to detect whether or not an object is already locked ?

UPDATE:

Thanks for mentioning the Lock interface, but that require programs to obey the same contract, i.e, they all refer to a Lock object instead of the synchronized keyword.

I wonder if there is a built-in way of checking the locking status ?

Thanks.

  • 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-02T04:44:20+00:00Added an answer on June 2, 2026 at 4:44 am

    Is there a fail-fast way of synchronization in Java?

    I think it is a poor choice of terminology to use “fail-fast” to describe what you are trying to do here. Fail-fast implies that not being to acquire a lock instantly is a failure or application error; c.f. fail-fast iterators which throw an unchecked CCME. That’s not the semantic model offered by locks in general, or here in particular. A better term would be “non-blocking”

    Also, it is not clear that silently skipping a block of code because you can’t acquire a lock is useful behavior. In most cases, the application needs to know that the “skip” path has been taken.

    These points aside, you can’t do it using primitive object locks.

    (OK, on some JVMs you might be able to use sun.misc.Unsafe to do this, but that’s a really bad idea. You are likely to find that your compiler, class loader or security sandbox stops you from using the Unsafe API … as it should. Besides, this API is not called “unsafe” for nothing!)

    The java.util.concurrent.locks.Lock API has a method that allows you to attempt to gain a lock without blocking. Specifically, the tryLock() method attempts to acquire the lock and immediately returns false if the lock is in use.

    There are other higher level concurrency classes that you could use as ersatz locks; e.g. Semaphore.


    Or put it another way, is there a way to detect whether or not an object is already locked ?

    Actually, that is a bit different … and not entirely useful either. Sure, you could (hypothetically) test if a lock is being held. (Indeed some Lock classes explicitly support this.) But that doesn’t mean that you’d be guaranteed to be able to acquire the lock without blocking. If you make that (incorrect) assumption, you’ve introduced a Heisenbug into your code.


    I wonder if there is a built-in way of checking the locking status ?

    [Assuming that you are referring to primitive locks … ]

    No there isn’t. At least, not within the running application itself. (A debug agent can do this, but it is not practical for an application to talk to its JVM’s debug agent.)

    If you want / need to do this kind of thing, you’ve got no real options that don’t involve changing your application’s locking mechanism. That’s the way it is.

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

Sidebar

Related Questions

Let's say i have this block of code, <div id=id1> This is some text
Let’s say I have a number like 0x448 . In binary this is 0100
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's say I have table with column 'URL' whrere I store urls like this
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say that I have a set of relations that looks like this: relations
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I can call a method like this: core::get() . What is the
Let's say there is a graph and some set of functions like: create-node ::

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.