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

The Archive Base Latest Questions

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

Say I have some code like so: private static Thing t = null; private

  • 0

Say I have some code like so:

private static Thing t = null;
private static final Object lock = new Object();   

public static void foo() {
    if(t == null) { //Netbeans warning on this line.
        synchronized(lock) {
            if(t == null) {
                t = new Thing();
            }
        }
    }
    ...Do stuff with t...
}

Now, foo is hosted on a server, and could possibly be called by many users at the same time. The purpose of the function foo is to initialize t if it hasn’t been already, otherwise, use the initialized instance to do whatever stuff needs to be done.

However, Netbeans gives me a warning on code like this:

Remove the outer conditional statement

With a tooltip:

Double-checked locking

The point of the double checked locking is to avoid a bottleneck waiting for people to release the lock. If the only check to see if t was null was inside of the synchronized block, everybody would have to wait for their chance to see if t was null, and everybody-1 would effectively wait for nothing (because t wouldn’t be null). Hence the outer conditional.

With it, say, by some miracle, 5 people resolve t == null to true at the same time. One user will get the synchronized lock, see that t == null is still true, and initialize it. Then, that user will release the lock, and the next user will lock on it, and almost immediately release it (as t != null anymore), this will continue until the 5 users have passed through it. All the while, other users connecting jump over the synchronized lock, as the first user in made t != null.

So, why is Netbeans complaining? This seems like the best way to do it AFAIK.

Thoughts?

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

    It’s complaining because as written, it’s unsafe – or at least may be.

    Because t isn’t volatile, it may be possible for a reader thread to read the new value of t while Thing is still being constructed. That would be a bad move – the use of partially-constructed objects is hideous.

    Before Java 5’s new memory model, it wouldn’t even have been thread-safe with the field marked as volatile. With the new memory model that’s safe in certain situations, potentially even without volatile, but personally I’d avoid it anyway:

    • Just synchronizing in every call isn’t likely to be a significant bottleneck in most applications
    • Usually initializing as part of the static initializer is good enough:

      private static Thing t = new Thing();
      
    • For other cases there are alternative tricks with nested classes which still use the laziness of static initializers, but all this single property to be lazy even when the other members are accessed.

    • For a straight singleton pattern, consider using enums instead.

    For a lot more information on the old brokenness of DCL, read The “Double-Checked Locking is Broken” Declaration. (See the end section for the current situation as of JDK 5 – but think about whether you really want maintainers of your code to be thinking about all of this.)

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

Sidebar

Related Questions

let's say we have some simple code like this : private static void Main()
Let's say I have some Java code: public class SomeClass { static { private
Say I have a method declaration like: private void someMethod(final String someKey, final Object
Say I have some code like namespace Portal { public class Author { public
Let's say we have some code like this running in the separate thread: private
Say I have some code like this: class Foo { public: Foo(int v) :
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
Let's say I have some code like this if(isset($_GET['foo'])) //do something if(isset($_GET['bar'])) //do something
In PHP, say that you have some code like this: $infrastructure = mt_rand(0,100); if
Let's say I have some code (using CherryPy) that looks like this: import cherrypy

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.