多线程基准测试Java中的多线程集合
我用JMH来衡量在收藏上花费的平均时间
当我试图实现多线程add
、get
和remove
时,我希望确保正确编写操作以使用此类结构
我将描述在列表中删除的想法
CopyOnWriteArrayList<Double>(collection)
Collections.synchronizedList(collection)
基准如下所示:
@Benchmark
public List<Double> testRemoveWithIndex(Data d, Blackhole blackhole) {
List<Double> obj = d.getCollection();
ParallelBenchmarkListsRemove.removeRandomInList(d.getCollection(), d.orderArray, d.objects, blackhole);
return obj;
}
使用removeRandomInList
方法:
public static <T> void removeRandomInList(List<T> filledList, int[] order, int noObjects, Blackhole blackhole) {
while (filledList.size() > 1) {
blackhole.consume(filledList.remove(order[noObjects - filledList.size()]));
}
}
类似的情况也出现在{
对于add
操作:
public static <T> void fillList(List<T> listToFill, T[] fillingArray, int noObjects) {
int i = 0;
while (listToFill.size() < noObjects) {
listToFill.add(fillingArray[i++]);
}
}
对于get
操作:
public static <T> void getRandomFromList(List<T> filledList, int[] order, int noObjects, Blackhole blackhole) {
int i = 0;
while (i * 10 < noObjects) {
blackhole.consume(filledList.get(order[i++]));
}
}
对于不同数量的线程(1、8、64),这种方法正确吗
共 (0) 个答案