Skip to content

Commit fa0a137

Browse files
committed
Merge branch '1.3.x'
2 parents 2ff9e3c + a244608 commit fa0a137

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/util/SimpleInMemoryRepository.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -18,29 +18,26 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.NavigableMap;
21+
import java.util.concurrent.ConcurrentHashMap;
2122
import java.util.concurrent.ConcurrentMap;
2223
import java.util.concurrent.ConcurrentNavigableMap;
2324
import java.util.concurrent.ConcurrentSkipListMap;
2425

25-
import org.springframework.util.ConcurrentReferenceHashMap;
26-
2726
/**
2827
* Repository utility that stores stuff in memory with period-separated String keys.
2928
*
3029
* @param <T> the type to store
3130
* @author Dave Syer
31+
* @author Andy Wilkinson
3232
*/
3333
public class SimpleInMemoryRepository<T> {
3434

3535
private ConcurrentNavigableMap<String, T> values = new ConcurrentSkipListMap<String, T>();
3636

37-
private final ConcurrentMap<String, Object> locks = new ConcurrentReferenceHashMap<String, Object>();
37+
private final ConcurrentMap<String, Object> locks = new ConcurrentHashMap<String, Object>();
3838

3939
public T update(String name, Callback<T> callback) {
40-
Object lock = this.locks.putIfAbsent(name, new Object());
41-
if (lock == null) {
42-
lock = this.locks.get(name);
43-
}
40+
Object lock = getLock(name);
4441
synchronized (lock) {
4542
T current = this.values.get(name);
4643
T value = callback.modify(current);
@@ -49,6 +46,18 @@ public T update(String name, Callback<T> callback) {
4946
}
5047
}
5148

49+
private Object getLock(String name) {
50+
Object lock = this.locks.get(name);
51+
if (lock == null) {
52+
Object newLock = new Object();
53+
lock = this.locks.putIfAbsent(name, newLock);
54+
if (lock == null) {
55+
lock = newLock;
56+
}
57+
}
58+
return lock;
59+
}
60+
5261
public void set(String name, T value) {
5362
T current = this.values.get(name);
5463
if (current != null) {

0 commit comments

Comments
 (0)