从 EDU 迁移到 java.util.concurrent 两次降低性能

Nei*_*our 5 java concurrency multithreading

来自http://forums.oracle.com/forums/thread.jspa?threadID=2195025&tstart=0 的交叉帖子

有一个电信应用服务器(基于 JAIN SLEE)和在其中运行的应用程序。
应用程序正在接收来自网络的消息,对其进行处理并将响应发送回网络。
请求/响应延迟的要求对于 95% 的调用是 250 毫秒,对于 99.999% 的调用是 3000 毫秒。
我们使用 EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap,1 个实例。对于一个调用(一个调用是多条消息)处理,调用以下方法:

"put", "get", "get", "get", then in 180 seconds "remove".
Run Code Online (Sandbox Code Playgroud)

有 4 个线程调用这些方法。
(小提示:使用 ConcurrentHashMap 不是唯一的活动。对于一个网络消息,还有很多其他活动:协议消息解析、查询数据库、将 SDR 写入文件、创建短期和长期对象。 )

当我们从 EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap 转移到 java.util.concurrent.ConcurrentHashMap 时,我们看到性能从每秒 1400 次调用降低到 800 次调用。
每秒最后 800 次调用的第一个瓶颈是无法满足上述要求的延迟。

这种性能下降会在具有以下 CPU 的主机上重现:

  • 2 CPU x Quad-Core AMD Opteron 2356 2312 MHz,共8个硬件线程,
  • 2 个 CPU x Intel Xeon E5410 2.33 GHz,总共 8 个硬件线程。

它不会在 X5570 CPU(Intel Xeon Nehalem X5570 2.93 GHz,总共 16 个硬件线程)上重现。

有没有人遇到过类似的问题?如何解决它们?

Pet*_*rey 2

我假设你花费的是纳秒而不是毫秒。(这比原来小一百万倍!)

或者使用 ConcurrentHashMap 是延迟的一小部分。

编辑:已将示例编辑为使用 100 个任务的多线程。

/*
Average operation time for a map of 10,000,000 was 48 ns
Average operation time for a map of 5,000,000 was 51 ns
Average operation time for a map of 2,500,000 was 48 ns
Average operation time for a map of 1,250,000 was 46 ns
Average operation time for a map of 625,000 was 45 ns
Average operation time for a map of 312,500 was 44 ns
Average operation time for a map of 156,200 was 38 ns
Average operation time for a map of 78,100 was 34 ns
Average operation time for a map of 39,000 was 35 ns
Average operation time for a map of 19,500 was 37 ns
 */
 public static void main(String... args) {
    ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    try {
        for (int size = 100000; size >= 100; size /= 2)
            test(es, size);
    } finally {
        es.shutdown();
    }
}

private static void test(ExecutorService es, final int size) {
    int tasks = 100;
    final ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<Integer, String>(tasks*size);
    List<Future> futures = new ArrayList<Future>();
    long start = System.nanoTime();
    for (int j = 0; j < tasks; j++) {
        final int offset = j * size;
        futures.add(es.submit(new Runnable() {
            public void run() {
                for (int i = 0; i < size; i++)
                    map.put(offset + i, "" + i);
                int total = 0;
                for (int j = 0; j < 10; j++)
                    for (int i = 0; i < size; i++)
                        total += map.get(offset + i).length();
                for (int i = 0; i < size; i++)
                    map.remove(offset + i);
            }
        }));
    }
    try {
        for (Future future : futures)
            future.get();
    } catch (Exception e) {
        throw new AssertionError(e);
    }
    long time = System.nanoTime() - start;
    System.out.printf("Average operation time for a map of %,d was %,d ns%n", size * tasks, time / tasks / 12 / size);
}
Run Code Online (Sandbox Code Playgroud)