Почему SpringApplicationBuilder.run () использует AtomicBoolean obj, а затем использует синхронизированный, AtomicBoolean не может обеспечить безопасность потока? - PullRequest
1 голос
/ 18 апреля 2019

Я прочитал исходный код SpringBoot в 1.5.X, но обнаружил проблему в классе SpringApplicationBuilder, это код метода run:

    /**
     * Create an application context (and its parent if specified) with the command line
     * args provided. The parent is run first with the same arguments if has not yet been
     * started.
     * @param args the command line arguments
     * @return an application context created from the current state
     */
    public ConfigurableApplicationContext run(String... args) {
        if (this.running.get()) {
            // If already created we just return the existing context
            return this.context;
        }
        configureAsChildIfNecessary(args);
        if (this.running.compareAndSet(false, true)) {
            synchronized (this.running) {
                // If not already running copy the sources over and then run.
                this.context = build().run(args);
            }
        }
        return this.context;
    }

, как вы видите использование метода runполе AtomicBoolean running для обеспечения безопасности потока, сравнить с AndSet не безопасно для потока?и зачем использовать синхронизированный?

if (this.running.compareAndSet(false, true)) {
    synchronized (this.running) {
        // thread safe
    }
}
if (this.running.compareAndSet(false, true)) {
    // not thread safe ?
}

что это значит в SpringApplicationBuilder.run()?

...