为什么SpringApplicationBuilder.run()使用AtomicBoolean obj然后使用同步的AtomicBoolean无法确保线程安全?

时间:2019-04-18 13:44:46

标签: java spring-boot synchronized atomicity

我在1.5.X中阅读了SpringBoot源代码,但是在类var str1 = '10,00 €'; var str2 = '12.22 $'; function getCurrencySymbol(str) { //replace all numbers, spaces, commas, and periods with an empty string //we should only be left with the currency symbols return str.replace(/[\d\., ]/g, ''); } console.log(getCurrencySymbol(str1)); console.log(getCurrencySymbol(str2));中发现了一个问题,它是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; } 方法使用AtomicBoolean字段run来保持线程安全,compareAndSet不是线程安全的吗?为什么要使用同步的?

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

if (this.running.compareAndSet(false, true)) { // not thread safe ? } 中这是什么意思?

0 个答案:

没有答案