Search code examples
javaswingawtswingworkerswingx

SwingWorker process() is not invoked for each publish()


I am new to Swings.

I have nearly 2 million of records in a txt file, need to read one-by-one process(split column based) it and push them to Table. It’s taking approximately 4-5 seconds to complete entire process.

Problem I am facing: JTable/UI is waiting until all 2 million records are getting processed and then displaying all together. Is there any option where I can push the records to JTable UI on batch based OR parallel pushing, like once I process 500 records, I can push those to UI ?

Tried to implement using SwingWorker, invoking push() method for each 500 records, but process() is invoked only once for all, not for each 500. Please correct if my approach is wrong.

new SwingWorker<Boolean, java.util.List<String>>() {
    @Override
    protected Boolean doInBackground() throws Exception {

        // Example Loop
        int i = 1;
        String line;
        java.util.List<String> list = new LinkedList();
        while ((line = in.readLine()) != null) {
            line = line.trim().replaceAll(" +", " ");
            list.add(line);

            if (i % 500 == 0) {
                publish(list);
                list = new LinkedList();
            }
            i++;
        }

        return true;
    }

    @Override
    protected void process(java.util.List<java.util.List<String>> chunks) {
        dtm.addRow(new String[]{"data"});
        System.out.println("Found even number: " + chunks.size());
    }
}.execute();

Thanks.


Solution

  • Swing is free to associate several calls to publish to a single call to process as the doc says:

    publish

    Because the process method is invoked asynchronously on the Event Dispatch Thread multiple invocations to the publish method might occur before the process method is executed. For performance purposes all these invocations are coalesced into one invocation with concatenated arguments. For example:

    publish("1");
    publish("2", "3");
    publish("4", "5", "6");
    

    might result in:

    process("1", "2", "3", "4", "5", "6")
    

    There is no need to publish every 500 iterations, publish as soon as a single result is available and let Swing coalesce as it wants.