Почему последний код tomcat использует синхронизированный в основном методе - PullRequest
0 голосов
/ 25 октября 2018

Просмотрите последний код Tomcat, найдите в основном методе, он использует синхронизированный при инициализации экземпляр начальной загрузки.

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);
        }
    } 

1 Ответ

0 голосов
/ 25 октября 2018

TL; DR: Марк Томас уменьшил количество предупреждений от инструмента статического анализа кода SpotBugs.

Как вы видите в журнале Tomcat SVN, синхронизация была введена в ревизии 1826336: https://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/Bootstrap.java?r1=1826335&r2=1826336&

Комментарий Fix some more SpotBugs warnings.

При запуске SpotBugs в ревизии 1826335 файла Bootstrap.java со всеми активированными модулями и максимальной чувствительностью / многословностью я мог воспроизвести предупреждение, которое он исправил:

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)
...