0

I have a JavaFX program where the user can load a XML file. This file is then being parsed using javax.xml.parser.DocumentBuilder on a worker thread that extends javafx.concurrent.Task.

Loading and parsing the xml files works just fine. The problem is that with some files the loading takes much longer than with others and I can't figure out what causes the loading process to take longer.

I measured the duration of the loading task for two different example files. File A has a size of around 80MB, the size of File B is approximately 50MB. Loading File A takes around 5s on average, while it's around 20s for File B. Both files have ~1,000,000 nodes with ~60% being white space text nodes. Also the file tree structure of both files is similar and processing the parsed org.w3c.dom.Document in further steps takes approximately an equal amount of time.

Another observation I made is, that when I'm loading File A the GUI has some serious lags, whereas it's running smoothly when loading File B. Because of this I concluded that the Thread loading File A has - in some mystic way - a higher priority than the Thread loading File B.

Here's the code snippet used for loading:

File file = fileChooser.showOpenDialog(stage);
Document doc;

private class ReadXmlTask extends Task<Void> {
    @Override
    protected Void call() {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(file);
        } catch ...

        return null;
    }
}

Thread thread = new Thread(new ReadXmlTask());
thread.setDaemon(true);
thread.start();

It doesn't matter whether setDaemon() is set to falseor true.

So my question is: Where does the different running time come from? And is there any way to set some sort of priority for a worker thread in comparison to the JavaFX Application Thread?

2
  • 2
    Could you create a small test program without GUI which loads the files and measure the times without any multithreading influences? Is the loading time the same as when loading with the GUI program? May 19, 2014 at 17:10
  • 1
    Silly of me to not do this before asking the question. I averaged runtime on 10 runs and File A needed ~4s, File B took ~18s to load... So after all, this behaviour has nothing to with JavaFX. Thanks for giving me the hint. May 20, 2014 at 7:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.