Merge branch '1.3.x'
This commit is contained in:
commit
fa0a137cd2
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2015 the original author or authors.
|
* Copyright 2012-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -18,29 +18,26 @@ package org.springframework.boot.actuate.metrics.util;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.NavigableMap;
|
import java.util.NavigableMap;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
import java.util.concurrent.ConcurrentNavigableMap;
|
import java.util.concurrent.ConcurrentNavigableMap;
|
||||||
import java.util.concurrent.ConcurrentSkipListMap;
|
import java.util.concurrent.ConcurrentSkipListMap;
|
||||||
|
|
||||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Repository utility that stores stuff in memory with period-separated String keys.
|
* Repository utility that stores stuff in memory with period-separated String keys.
|
||||||
*
|
*
|
||||||
* @param <T> the type to store
|
* @param <T> the type to store
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
|
* @author Andy Wilkinson
|
||||||
*/
|
*/
|
||||||
public class SimpleInMemoryRepository<T> {
|
public class SimpleInMemoryRepository<T> {
|
||||||
|
|
||||||
private ConcurrentNavigableMap<String, T> values = new ConcurrentSkipListMap<String, T>();
|
private ConcurrentNavigableMap<String, T> values = new ConcurrentSkipListMap<String, T>();
|
||||||
|
|
||||||
private final ConcurrentMap<String, Object> locks = new ConcurrentReferenceHashMap<String, Object>();
|
private final ConcurrentMap<String, Object> locks = new ConcurrentHashMap<String, Object>();
|
||||||
|
|
||||||
public T update(String name, Callback<T> callback) {
|
public T update(String name, Callback<T> callback) {
|
||||||
Object lock = this.locks.putIfAbsent(name, new Object());
|
Object lock = getLock(name);
|
||||||
if (lock == null) {
|
|
||||||
lock = this.locks.get(name);
|
|
||||||
}
|
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
T current = this.values.get(name);
|
T current = this.values.get(name);
|
||||||
T value = callback.modify(current);
|
T value = callback.modify(current);
|
||||||
|
|
@ -49,6 +46,18 @@ public class SimpleInMemoryRepository<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Object getLock(String name) {
|
||||||
|
Object lock = this.locks.get(name);
|
||||||
|
if (lock == null) {
|
||||||
|
Object newLock = new Object();
|
||||||
|
lock = this.locks.putIfAbsent(name, newLock);
|
||||||
|
if (lock == null) {
|
||||||
|
lock = newLock;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lock;
|
||||||
|
}
|
||||||
|
|
||||||
public void set(String name, T value) {
|
public void set(String name, T value) {
|
||||||
T current = this.values.get(name);
|
T current = this.values.get(name);
|
||||||
if (current != null) {
|
if (current != null) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue