我正在尝试更改报告的执行并使其同时完成。在“串行模式”中,执行测试需要 30 秒,而在使用并发模式时,我需要 27 秒(考虑到必须串行执行几个步骤,我对结果没问题)。
我仍然没有得到的是这一行:
ExecutorService executor = Executors.newFixedThreadPool(4);
我的计算机配备了 2x2.6 Ghz 四核,如果 newFixedThreadPool 很高 (16),我希望执行时间会减少。实际上,我增加的 newFixedThreadPool 越多,执行速度就越慢。这就引出了一个问题:我做错了什么或者我没有理解什么?!?!
我从我的执行中嵌入了 2 个结果屏幕截图。
A. newSingleThreadExecuter - 在 23 秒内运行
B. newFixedThreadPool(4) - 在 43 秒内运行。
每次我提交“工人”时,我都会得到 system.out currentTimeMillis,“fatched tkt”结果是从数据库获取数据所需的毫秒数。(在策略 A 中 - 大约需要 3 毫秒,在 B 中最多需要 7 毫秒)。
    Stopper stopper = new Stopper();
    for (Long iNum : multimap.asMap().keySet())
    {
        List<Long> tickets = (List<Long>) multimap.get(iNum);
        for (Long ticketNumber : tickets)
        {
                pojoPks = getPkData(iNum);
                Callable<PojoTicket> worker = new MaxCommThread(ticketNumber, pojoPks);
                Future<PojoTicket> submit = executor.submit(worker);
                futures.add(submit);
        }
    }
    System.out.println("futurues: " +futures.size());
    for (Future<PojoTicket> future : futures)
    {
        try
        {
            PojoTicket pojoTicket = future.get();
            //do the rest here
        } catch (InterruptedException e)
        {
            System.out.println("---------------------->InterruptedException");
        } catch (ExecutionException e)
        {
            System.out.println("---------------------->ExecutionException");
        }
    }
    executor.shutdown();
    stopper.stop();

