0

review the latest code of Tomcat,find in the main method ,it use the synchronized when init the bootstrap instance.

synchronized (daemonLock) {
        if (daemon == null) {
            // Don't set daemon until init() has completed
            Bootstrap bootstrap = new Bootstrap();
            try {
                bootstrap.init();
            } catch (Throwable t) {
                handleThrowable(t);
                t.printStackTrace();
                return;
            }
            daemon = bootstrap;
        } else {
            // When running as a service the call to stop will be on a new
            // thread so make sure the correct class loader is used to
            // prevent a range of class not found exceptions.
            Thread.currentThread().setContextClassLoader(daemon.catalinaLoader);
        }
    } 
Rafael
  • 7,605
  • 13
  • 31
  • 46
skystmm
  • 1
  • 1

1 Answers1

0

TL;DR: Mark Thomas reduced warning messages from static code analysis tool SpotBugs.

As you cen see in Tomcat SVN log, the synchronize was introduced in revision 1826336: https://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/Bootstrap.java?r1=1826335&r2=1826336&

Commented with Fix some more SpotBugs warnings.

When running SpotBugs on revision 1826335 of Bootstrap.java with all modules activated and maximum sensitivity/verbosity, I may have reproduced the warning he has fixed:

Bug: Incorrect lazy initialization of static field Bootstrap.daemon in Bootstrap.main(String[])

This method contains an unsynchronized lazy initialization of a non-volatile static field. Because the compiler or processor may reorder instructions, threads are not guaranteed to see a completely initialized object, if the method can be called by multiple threads. You can make the field volatile to correct the problem. For more information, see the Java Memory Model web site. 

Rank: Of Concern (17), confidence: Low
Pattern: LI_LAZY_INIT_STATIC 
Type: LI, Category: MT_CORRECTNESS (Multithreaded correctness)
Selaron
  • 6,105
  • 4
  • 31
  • 39