|
|
|
@ -16,9 +16,6 @@
|
|
|
|
|
|
|
|
|
|
package com.zaxxer.hikari.util;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
import java.util.concurrent.atomic.LongAdder;
|
|
|
|
|
|
|
|
|
@ -49,22 +46,13 @@ public interface Sequence
|
|
|
|
|
{
|
|
|
|
|
public static Sequence create()
|
|
|
|
|
{
|
|
|
|
|
if (UtilityElf.isJdk8Plus() && !Boolean.getBoolean("com.zaxxer.hikari.useAtomicLongSequence")) {
|
|
|
|
|
if (!Boolean.getBoolean("com.zaxxer.hikari.useAtomicLongSequence")) {
|
|
|
|
|
return new Java8Sequence();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
try {
|
|
|
|
|
Class<?> longAdderClass = Sequence.class.getClassLoader().loadClass("com.codahale.metrics.LongAdder");
|
|
|
|
|
return new DropwizardSequence(longAdderClass);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e2) {
|
|
|
|
|
// fall thru
|
|
|
|
|
}
|
|
|
|
|
return new Java7Sequence();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Java7Sequence();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class Java7Sequence extends AtomicLong implements Sequence {
|
|
|
|
@ -80,42 +68,4 @@ public interface Sequence
|
|
|
|
|
return this.sum();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class DropwizardSequence implements Sequence {
|
|
|
|
|
private final Object longAdder;
|
|
|
|
|
private final Method increment;
|
|
|
|
|
private final Method sum;
|
|
|
|
|
|
|
|
|
|
public DropwizardSequence(Class<?> longAdderClass) throws Exception {
|
|
|
|
|
Constructor<?> constructor = longAdderClass.getDeclaredConstructors()[0];
|
|
|
|
|
constructor.setAccessible(true);
|
|
|
|
|
increment = longAdderClass.getMethod("increment");
|
|
|
|
|
increment.setAccessible(true);
|
|
|
|
|
sum = longAdderClass.getMethod("sum");
|
|
|
|
|
sum.setAccessible(true);
|
|
|
|
|
longAdder = constructor.newInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void increment()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
increment.invoke(longAdder);
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public long get()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return (Long) sum.invoke(longAdder);
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|