Remove unnecessary synchronization

- on AtomicBoolean in SpringApplicationBuilder
- on SimpleFormatter
- in a private method in FileSystemWatcher which is always called in a
  synchronized block
- Replaced synchronized guarded HashMap with ConcurrentHashMap
This commit is contained in:
Moritz Halbritter 2023-08-03 17:33:52 +02:00
parent 1a8b8ce26e
commit 726d2e6678
4 changed files with 14 additions and 27 deletions

View File

@ -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");
}
/**

View File

@ -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<String, Object> attributes = new HashMap<>();
private final Map<String, Object> attributes = new ConcurrentHashMap<>();
private final BlockingDeque<LeakSafeThread> 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);
}
/**

View File

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

View File

@ -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) {