diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java index 99ef112ea3..35a950c6e3 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -158,9 +158,7 @@ public class FileSystemWatcher { } private void checkNotStarted() { - synchronized (this.monitor) { - Assert.state(this.watchThread == null, "FileSystemWatcher already started"); - } + Assert.state(this.watchThread == null, "FileSystemWatcher already started"); } /** diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java index bdc69f02d8..410b269d1d 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,6 @@ import java.lang.reflect.Field; import java.net.URL; import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; @@ -30,6 +29,7 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.BlockingDeque; import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadFactory; @@ -92,7 +92,7 @@ public class Restarter { private final ClassLoaderFiles classLoaderFiles = new ClassLoaderFiles(); - private final Map attributes = new HashMap<>(); + private final Map attributes = new ConcurrentHashMap<>(); private final BlockingDeque leakSafeThreads = new LinkedBlockingDeque<>(); @@ -440,18 +440,11 @@ public class Restarter { } public Object getOrAddAttribute(String name, final ObjectFactory objectFactory) { - synchronized (this.attributes) { - if (!this.attributes.containsKey(name)) { - this.attributes.put(name, objectFactory.getObject()); - } - return this.attributes.get(name); - } + return this.attributes.computeIfAbsent(name, (ignore) -> objectFactory.getObject()); } public Object removeAttribute(String name) { - synchronized (this.attributes) { - return this.attributes.remove(name); - } + return this.attributes.remove(name); } /** diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java index 712caa5e6f..b479568fcb 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java @@ -76,7 +76,7 @@ public class SpringApplicationBuilder { private final SpringApplication application; - private ConfigurableApplicationContext context; + private volatile ConfigurableApplicationContext context; private SpringApplicationBuilder parent; @@ -145,10 +145,8 @@ public class SpringApplicationBuilder { } 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); - } + // If not already running copy the sources over and then run. + this.context = build().run(args); } return this.context; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/SimpleFormatter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/SimpleFormatter.java index 1701a6a03a..fc9dbfbcaf 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/SimpleFormatter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/java/SimpleFormatter.java @@ -38,17 +38,15 @@ public class SimpleFormatter extends Formatter { private final String pid = getOrUseDefault(LoggingSystemProperty.PID.getEnvironmentVariableName(), "????"); - private final Date date = new Date(); - @Override - public synchronized String format(LogRecord record) { - this.date.setTime(record.getMillis()); + public String format(LogRecord record) { + Date date = new Date(record.getMillis()); String source = record.getLoggerName(); String message = formatMessage(record); String throwable = getThrowable(record); String thread = getThreadName(); - return String.format(this.format, this.date, source, record.getLoggerName(), - record.getLevel().getLocalizedName(), message, throwable, thread, this.pid); + return String.format(this.format, date, source, record.getLoggerName(), record.getLevel().getLocalizedName(), + message, throwable, thread, this.pid); } private String getThrowable(LogRecord record) {