Revise synchronized blocks
- Replace synchronized with Lock when guarding long-running operations - Remove unnecessary synchronization in FileSystemWatcher - Replace HashMap with ConcurrentHashMap in Restarter - Remove unnecessary locking on AtomicBoolean in SpringApplicationBuilder - Remove unnecessary locking in SimpleFormatter Closes gh-36670
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 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.
|
||||
@@ -21,6 +21,8 @@ import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.springframework.core.io.FileSystemResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -44,6 +46,11 @@ public class SpringBootMockServletContext extends MockServletContext {
|
||||
|
||||
private File emptyRootDirectory;
|
||||
|
||||
/**
|
||||
* Guards access to {@link #emptyRootDirectory}.
|
||||
*/
|
||||
private final Lock emptyRootDirectoryLock = new ReentrantLock();
|
||||
|
||||
public SpringBootMockServletContext(String resourceBasePath) {
|
||||
this(resourceBasePath, new FileSystemResourceLoader());
|
||||
}
|
||||
@@ -91,19 +98,21 @@ public class SpringBootMockServletContext extends MockServletContext {
|
||||
if (resource == null && "/".equals(path)) {
|
||||
// Liquibase assumes that "/" always exists, if we don't have a directory
|
||||
// use a temporary location.
|
||||
this.emptyRootDirectoryLock.lock();
|
||||
try {
|
||||
if (this.emptyRootDirectory == null) {
|
||||
synchronized (this) {
|
||||
File tempDirectory = Files.createTempDirectory("spr-servlet").toFile();
|
||||
tempDirectory.deleteOnExit();
|
||||
this.emptyRootDirectory = tempDirectory;
|
||||
}
|
||||
File tempDirectory = Files.createTempDirectory("spr-servlet").toFile();
|
||||
tempDirectory.deleteOnExit();
|
||||
this.emptyRootDirectory = tempDirectory;
|
||||
}
|
||||
return this.emptyRootDirectory.toURI().toURL();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// Ignore
|
||||
}
|
||||
finally {
|
||||
this.emptyRootDirectoryLock.unlock();
|
||||
}
|
||||
}
|
||||
return resource;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user