具有以下要求的高性能并发收集的合适候选者是什么:
我考虑使用ConcurrentLinkedQueue<>为此,但如果你从不调用poll()方法,发现它会泄漏内存(按设计)。我不确定是否仍然如此(提到这些的帖子来自〜2011年,我发现有人提到这可能已经解决了。)
我还考虑过ConcurrentSkipListSet<>,但我不确定排序对性能的影响是什么(因为我不关心顺序)。
答案 0 :(得分:6)
如果您正在寻找“足够快”的解决方案,可以使用ConcurrentLinkedQueue。其Iterator.remove()中众所周知的内存泄漏问题已被修复为http://bugs.java.com/view_bug.do?bug_id=6785442的一部分。现在删除了ConcurrentLinkedQueue $ Node应该已成功GC。但如果您正在寻找性能最佳的解决方案,那么......
根本不要使用迭代器(并且因此也要使用Collection),因为Collection.iterator()准备了Iterator的新实例,而btw,它的next()方法不是免费的:每次使用Iterator迭代一个集合时,至少花费CPU时间:大约10条指令为新对象分配内存+一些构造函数的指令(参见ConcurrentLinkedQueue $ Itr的源代码)+ ConcurrentLinkedQueue $ Itr.next( )+在次要GC上从伊甸园中移除物体。
普通数组+直接索引是最快的迭代技术。因此,使用CopyOnWriteArrayList或实现您自己的集合来使用普通数组迭代许多项。例如,如果您很少添加/删除项目,并且您希望在迭代时删除它们而不是按索引删除它们,您可以尝试类似以下内容:
public enum IterationResult {
NEXT, REMOVE, BREAK;
}
public interface CollectionIterator<T> {
IterationResult onObject(T object);
}
public interface CollectionModification<T> {
CollectionModification<T> add(T item);
CollectionModification<T> remove(T item);
}
public class MyCollection<T> {
private volatile State state;
private final ReentrantLock modificationLock = new ReentrantLock();
private State currentModification;
public MyCollection() {
this(10);
}
public MyCollection(int initialSize) {
state = new State(initialSize);
}
public CollectionModification<T> startModification() {
modificationLock.lock();
currentModification = new State(state);
return currentModification;
}
public void finishModification() {
state = currentModification;
modificationLock.unlock();
}
@SuppressWarnings("unchecked")
public void iterate(CollectionIterator<T> it) {
final State currentState = state;
State modifiedState = null;
try {
out_:
for (int i = 0; i < currentState.size; i++) {
final T item = (T) currentState.items[i]; // unchecked
final IterationResult result = it.onObject(item);
switch (result) {
case BREAK:
break out_;
case REMOVE:
if (modifiedState == null) {
modifiedState = (State) startModification();
}
modifiedState.removeExactly(item);
break;
default:
break;
}
}
} finally {
if (modifiedState != null) {
finishModification();
}
}
}
private class State implements CollectionModification<T> {
private Object[] items;
private int size;
private State(int initialSize) {
items = new Object[initialSize];
}
private State(State from) {
items = new Object[from.items.length];
size = from.size;
System.arraycopy(from.items, 0, items, 0, size);
}
@Override
public CollectionModification<T> add(T item) {
if (size == items.length) {
final Object[] newItems = new Object[size + size >>> 1];
System.arraycopy(items, 0, newItems, 0, size);
items = newItems;
}
items[size] = item;
size++;
return this;
}
@Override
public CollectionModification<T> remove(T item) {
for (int i = 0; i < size; i++) {
if (Objects.equals(item, items[i])) {
removeItem(i);
break;
}
}
return this;
}
private void removeExactly(T item) {
for (int i = 0; i < size; i++) {
if (item == items[i]) {
removeItem(i);
break;
}
}
}
private void removeItem(int index) {
if (index < items.length - 1) {
System.arraycopy(items, index + 1, items, index, size - 1);
}
size--;
}
}
}
用法:
CollectionIterator<Integer> remove2 = new CollectionIterator<Integer>() {
@Override
public IterationResult onObject(Integer object) {
return object == 2 ? IterationResult.REMOVE : IterationResult.NEXT;
}
};
MyCollection<Integer> col = new MyCollection<>();
CollectionModification<Integer> mod = col.startModification();
try {
mod.add(new Integer(1))
.add(new Integer(2))
.add(new Integer(3));
} finally {
col.finishModification();
}
col.iterate(remove2);
这与CopyOnWriteArrayList非常相似。顺便说一句,如果你只有一个修改集合的线程(单个编写器)和许多读者,你就可以摆脱锁定,因为volatile
足以保证作者和所有读者之间所有变化的可见性。此外,如果延迟对您很重要,您可以通过忙等待替换经典锁,以获得无锁收集。
您应该了解的主要事项是,在许多情况下,针对特定要求的最高性能解决方案是编写一段您自己的微调代码。这是不付你没用的东西的方法。这就是为什么高性能/低延迟应用程序通常不会在其主要路径中使用常见的第三方库
答案 1 :(得分:1)
该计划:
import java.util.*;
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
testCollection(new ArrayList<Integer>());
testCollection(Collections.synchronizedList(new ArrayList<Integer>()));
testCollection(new CopyOnWriteArrayList<Integer>());
testCollection(new LinkedList<Integer>());
testCollection(Collections.synchronizedList(new LinkedList<Integer>()));
testCollection(Collections.newSetFromMap(new ConcurrentHashMap<Integer, Boolean>()));
testCollection(new ConcurrentLinkedQueue<Integer>());
testCollection(new ConcurrentSkipListSet<Integer>());
}
static void testCollection(Collection<Integer> collection) {
testCollection(collection, 3);
}
static void testCollection(Collection<Integer> collection, int count) {
Test t = new Test(collection);
for (int i = 0; i < 10; i++)
System.gc();
while (count > 0) {
long time = 0, iterationTime;
for (int x = 0; x < 1010; x++) {
iterationTime = t.timeIteration();
if (x >= 10) { // skip first 10 runs results to reduce the effect of runtime optimizations
time += iterationTime;
}
}
System.out.println(collection.getClass() + ": " + time / 1000000.0 + " milliseconds");
count--;
}
}
static class Test {
private Collection<Integer> list;
public Test(Collection<Integer> list) {
list.addAll(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
this.list = list;
}
long timeIteration() {
Iterator<Integer> iterator;
long start = System.nanoTime();
for (int i = 0; i < 10000; i++) {
for (iterator = list.iterator(); iterator.hasNext(); ) {
Integer x = iterator.next();
if (x > 20)
System.out.println("this doesn't happen");
}
}
return System.nanoTime() - start;
}
}
}
结果:(为清晰起见而格式化)
╔══════════════════════════════════════════╦══════════════╗
║ class (java.util.) ║ milliseconds ║
╠══════════════════════════════════════════╬══════════════╣
║ ArrayList ║ 138.242208 ║
║------------------------------------------║--------------║
║ ArrayList ║ 135.795334 ║
║------------------------------------------║--------------║
║ ArrayList ║ 160.516023 ║
║------------------------------------------║--------------║
║ Collections$SynchronizedRandomAccessList ║ 371.29873 ║
║------------------------------------------║--------------║
║ Collections$SynchronizedRandomAccessList ║ 318.442416 ║
║------------------------------------------║--------------║
║ Collections$SynchronizedRandomAccessList ║ 335.079316 ║
║------------------------------------------║--------------║
║ concurrent.CopyOnWriteArrayList ║ 299.203427 ║
║------------------------------------------║--------------║
║ concurrent.CopyOnWriteArrayList ║ 361.800762 ║
║------------------------------------------║--------------║
║ concurrent.CopyOnWriteArrayList ║ 316.672923 ║
║------------------------------------------║--------------║
║ LinkedList ║ 776.843317 ║
║------------------------------------------║--------------║
║ LinkedList ║ 807.458514 ║
║------------------------------------------║--------------║
║ LinkedList ║ 793.940155 ║
║------------------------------------------║--------------║
║ Collections$SynchronizedList ║ 793.125156 ║
║------------------------------------------║--------------║
║ Collections$SynchronizedList ║ 782.003326 ║
║------------------------------------------║--------------║
║ Collections$SynchronizedList ║ 790.937425 ║
║------------------------------------------║--------------║
║ Collections$SetFromMap ║ 1452.049046 ║
║------------------------------------------║--------------║
║ Collections$SetFromMap ║ 1457.275127 ║
║------------------------------------------║--------------║
║ Collections$SetFromMap ║ 1450.322531 ║
║------------------------------------------║--------------║
║ concurrent.ConcurrentLinkedQueue ║ 976.803439 ║
║------------------------------------------║--------------║
║ concurrent.ConcurrentLinkedQueue ║ 855.64423 ║
║------------------------------------------║--------------║
║ concurrent.ConcurrentLinkedQueue ║ 845.05258 ║
║------------------------------------------║--------------║
║ concurrent.ConcurrentSkipListSet ║ 926.423234 ║
║------------------------------------------║--------------║
║ concurrent.ConcurrentSkipListSet ║ 924.370203 ║
║------------------------------------------║--------------║
║ concurrent.ConcurrentSkipListSet ║ 933.504106 ║
╚══════════════════════════════════════════╩══════════════╝
欢迎提出意见。