E
E
Egor Khombak2021-10-04 11:08:40
Java
Egor Khombak, 2021-10-04 11:08:40

Why are elements added to the ArrayList happening in batches of 10 elements?

There is a task - to parse a bunch of objects, a little more than 200k and add them to the sheet. Here is the log of adding elements


[2021-10-04 10:58:05,213]-[export-1] INFO - new object process: 32060
[2021-10-04 10:58:05,418]-[export-1] INFO - new object process: 32061
[2021-10-04 10:58:05,418]-[export-1] INFO - new object process: 32062
[2021-10-04 10:58:05,418]-[export-1] INFO - new object process: 32063
[2021-10-04 10:58:05,418]-[export-1] INFO - new object process: 32064
[2021-10-04 10:58:05,418]-[export-1] INFO - new object process: 32065
[2021-10-04 10:58:05,418]-[export-1] INFO - new object process: 32066
[2021-10-04 10:58:05,418]-[export-1] INFO - new object process: 32067
[2021-10-04 10:58:05,418]-[export-1] INFO - new object process: 32068
[2021-10-04 10:58:05,418]-[export-1] INFO - new object process: 32069
[2021-10-04 10:58:05,418]-[export-1] INFO - new object process: 32070
[2021-10-04 10:58:05,622]-[export-1] INFO - new object process: 32071

If you pay attention to the time of adding objects, you can see that they are added in batches of 10 pieces literally in 1-2 thousandths of a second, but the break after adding 10 records is 0.2-0.5 seconds. Initially, I thought that this was due to the standard sheet capacity of 10 elements. So the sheet is initialized immediately for 200000 capacity.
List<exportObject> objects = new ArrayList<>(200000);
        int i = 0;
        while (resultSet.next()) {
            log.info("new object process: {}", ++i);
            Integer lvl = resultSet.getInt("lvl");
            String objectId = resultSet.getString("object_id");
            int orderNumber = resultSet.getInt("order_number");
            objects.add(new exportObject(lvl, objectId, orderNumber));
        }

What is the reason for this delay between every 10 elements?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Osipov, 2021-10-05
@Khombachke

Maybe the database driver immediately pulls the next 10 objects from the ResultSet, if any. When they end, the driver goes back to the database for new data
. Something like this https://docs.oracle.com/cd/E11882_01/java.112/e165...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question