I am currently using a SwingWorker
to prevent the Swing GUI from hanging up when it's going a task that takes a while to complete (Web Scraping). Additionally, I'm calling the publish()
method from doInBackground()
to send Interim data chunks to the process method as seen on the example code below. However, this is causing me problems as the process method is done asynchronously and can sometimes be overwritten by a new data chunk which causes some of the data to disappear. Is it possible to tell doInBackground()
to wait for the process/publish method to finish and make sure that the data is processed and used as required before continuing and possibly overwriting the previous data?
class validateSources extends SwingWorker<Void, Integer> {
@Override
protected Void doInBackground() throws Exception {
for (int i = 0; i < size; i++) {
//Do some webscraping here
//Publish the data to the process method
publish(webscrapingData);
}
}
@Override
protected void process(List<Integer> chunks) {
//Use WebscrapingData for something
Object webscraingData = chunks.get(0);
}
}
EDIT:
I noticed that the details I previously gave were not sufficient. The issue I'm currently having is that it's crucial for the data being published from doInBackground() to be fully processed before the next iteration is continued in doInBackground(). Thus, I'm looking for a way to ensure that the code in process() fully completes before doing the next iteration of Webscraping.
Read the section from the Swing tutorial on Tasks That Have Interim Results. You will find a statement that says:
Results from multiple invocations of publish are often accumulated for a single invocation of process.
Object webscraingData = chunks.get(0);
You only process a single item.
You need to iterate through all the items in the list.