java - 从 EDU 迁移到 java.util.concurrent 会降低性能两倍

标签 java multithreading concurrency

来自 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".

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

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

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

  • 2 个 CPU x 四核 AMD Opteron 2356 2312 MHz,总共 8 个 HW 线程,
  • 2 个 CPU x 英特尔至强 E5410 2.33 GHz,8 硬件线程总数。

它没有在 X5570 CPU(Intel Xeon Nehalem X5570 2.93 GHz,总共 16 个 HW 线程)上重现。

有人遇到过类似的问题吗?如何解决?

最佳答案

我假设您花费的时间大约是纳秒而不是毫秒。 (这是小一百万倍!)

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);
}

关于java - 从 EDU 迁移到 java.util.concurrent 会降低性能两倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5404603/

相关文章:

java - FlexJSON 默认按字母顺序排序

java - 使用哪些语言/互联网协议(protocol)来远程控制机器人/电子产品?

java - JAAS登录模块中的用户主体-额外的属性是单独的主体还是Bean属性?

javascript - javascript 中的 Promise 底层是如何工作的?我的 Promise 实现效果不一样

sockets - 如果消息发布(启用确认)到具有免费消费者的队列,RabbitMQ 是否会保证交付?

java - 为什么我的多线程 Java 程序没有最大化我机器上的所有内核?

java - 在字符串中找到一个 6 位数字

java - 按名称获取线程

Java 8 并行流实际上并没有并行工作

multithreading - pthread_cond_wait 在等待时自动并原子地解锁互斥体