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 false
or 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?
A
needed ~4s, FileB
took ~18s to load... So after all, this behaviour has nothing to with JavaFX. Thanks for giving me the hint.