Skip to content

Commit fbd3b65

Browse files
committed
Refactor synchronized to Lock in ApplicationTemp
The synchronized guards an I/O operation. Additionally, this adds double-checked locking to the path instance field. See gh-36670
1 parent bcee354 commit fbd3b65

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationTemp.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,8 @@
2828
import java.security.MessageDigest;
2929
import java.util.EnumSet;
3030
import java.util.HexFormat;
31+
import java.util.concurrent.locks.Lock;
32+
import java.util.concurrent.locks.ReentrantLock;
3133

3234
import org.springframework.util.Assert;
3335
import org.springframework.util.StringUtils;
@@ -49,6 +51,8 @@ public class ApplicationTemp {
4951

5052
private final Class<?> sourceClass;
5153

54+
private final Lock pathLock = new ReentrantLock();
55+
5256
private volatile Path path;
5357

5458
/**
@@ -90,9 +94,15 @@ public File getDir(String subDir) {
9094

9195
private Path getPath() {
9296
if (this.path == null) {
93-
synchronized (this) {
94-
String hash = HexFormat.of().withUpperCase().formatHex(generateHash(this.sourceClass));
95-
this.path = createDirectory(getTempDirectory().resolve(hash));
97+
this.pathLock.lock();
98+
try {
99+
if (this.path == null) {
100+
String hash = HexFormat.of().withUpperCase().formatHex(generateHash(this.sourceClass));
101+
this.path = createDirectory(getTempDirectory().resolve(hash));
102+
}
103+
}
104+
finally {
105+
this.pathLock.unlock();
96106
}
97107
}
98108
return this.path;

0 commit comments

Comments
 (0)