有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程基准测试Java中的多线程集合

我用JMH来衡量在收藏上花费的平均时间

当我试图实现多线程addgetremove时,我希望确保正确编写操作以使用此类结构

我将描述在列表中删除的想法

  1. CopyOnWriteArrayList<Double>(collection)
  2. 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) 个答案