Skip to content

Commit d1a3cd5

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Under the JRE, use java.util.concurrent.atomic.LongAdder unconditionally and (except when we need to support GWT/J2CL) directly instead of through our LongAddable interfaces.
It's available [as of Java 8](https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/util/concurrent/atomic/LongAdder.html). This eliminates our [usages of `Unsafe`](#6806) in `Striped64`, a part of the implementation of our `LongAdder`. On the Android side, we'll have to wait [until we require API Level 24](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder). RELNOTES=n/a PiperOrigin-RevId: 713268966
1 parent 400af25 commit d1a3cd5

File tree

11 files changed

+25
-1250
lines changed

11 files changed

+25
-1250
lines changed

guava-gwt/src-super/com/google/common/cache/super/com/google/common/cache/LongAddables.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323
*/
2424
final class LongAddables {
2525
public static LongAddable create() {
26-
return new LongAdder();
26+
return new GwtLongAddable();
27+
}
28+
29+
private static final class GwtLongAddable implements LongAddable {
30+
private long value;
31+
32+
public void increment() {
33+
value++;
34+
}
35+
36+
public void add(long x) {
37+
value += x;
38+
}
39+
40+
public long sum() {
41+
return value;
42+
}
2743
}
2844
}

guava-gwt/src-super/com/google/common/cache/super/com/google/common/cache/LongAdder.java

-39
This file was deleted.

guava-tests/test/com/google/common/cache/LongAdderTest.java

-42
This file was deleted.

guava/src/com/google/common/cache/LongAddables.java

+4-46
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,18 @@
1515
package com.google.common.cache;
1616

1717
import com.google.common.annotations.GwtCompatible;
18-
import com.google.common.base.Supplier;
19-
import java.util.concurrent.atomic.AtomicLong;
18+
import java.util.concurrent.atomic.LongAdder;
2019

2120
/**
22-
* Source of {@link LongAddable} objects that deals with GWT, Unsafe, and all that.
21+
* Source of {@link LongAddable} objects that deals with GWT and all that.
2322
*
2423
* @author Louis Wasserman
2524
*/
2625
@GwtCompatible(emulated = true)
2726
final class LongAddables {
28-
private static final Supplier<LongAddable> SUPPLIER;
29-
30-
static {
31-
Supplier<LongAddable> supplier;
32-
try {
33-
// trigger static initialization of the LongAdder class, which may fail
34-
LongAdder unused = new LongAdder();
35-
supplier =
36-
new Supplier<LongAddable>() {
37-
@Override
38-
public LongAddable get() {
39-
return new LongAdder();
40-
}
41-
};
42-
} catch (Throwable t) { // we really want to catch *everything*
43-
supplier =
44-
new Supplier<LongAddable>() {
45-
@Override
46-
public LongAddable get() {
47-
return new PureJavaLongAddable();
48-
}
49-
};
50-
}
51-
SUPPLIER = supplier;
52-
}
53-
5427
public static LongAddable create() {
55-
return SUPPLIER.get();
28+
return new JavaUtilConcurrentLongAdder();
5629
}
5730

58-
private static final class PureJavaLongAddable extends AtomicLong implements LongAddable {
59-
@Override
60-
public void increment() {
61-
getAndIncrement();
62-
}
63-
64-
@Override
65-
public void add(long x) {
66-
getAndAdd(x);
67-
}
68-
69-
@Override
70-
public long sum() {
71-
return get();
72-
}
73-
}
31+
private static final class JavaUtilConcurrentLongAdder extends LongAdder implements LongAddable {}
7432
}

guava/src/com/google/common/cache/LongAdder.java

-193
This file was deleted.

0 commit comments

Comments
 (0)