Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are the publish() and process() methods on SwingWorker properly used?

These two methods in SwingWorker are confusing me at the moment, and it seems like it is very easy to use them incorrectly.

The method publish() describes the following:

Sends data chunks to the process(java.util.List) method. This method is to be used from inside the doInBackground method to deliver intermediate results for processing on the Event Dispatch Thread inside the process method.

What this means to me is that while my worker thread is executing its doInBackground() method, I am able to create "chunks" (should these be anything specific or is this just a way to refer to message objects?), then publish them for processing on my Swing GUI.

That leads me to process(). The javadoc outlines the following:

Receives data chunks from the publish method asynchronously on the Event Dispatch Thread.

Having looked over the documentation for both methods, could anybody clarify what the mechanism is behind how this is taking place? I understand that it is an asynchronous process as per the documentation but since it is taking place on the EDT I am picturing there being some predictability to it.

The publish() documentation states this:

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.

To summarize, my question is two fold:

  • Who should be calling process()?
  • What is the workflow for process() in the context of the SwingWorker and the EDT?

Please let me know if any clarification is needed.

like image 637
Surveon Avatar asked Aug 30 '13 14:08

Surveon


2 Answers

I wrote a program that processes directories in a subtree. The processing takes long enough that the user needs some feedback that something is going on. I put up a dialog that contains a label with the name of the subdirectory currently being processed; the swingworker thread called publish() with the name of each directory being processed.

There is no guarantee that the UI will be updated for each directory name -- sometimes the processing goes faster than the redisplay, especially if anything else is happening on the computer, and depending on the computer on which things run. So publish() might be called and execute several times without process() being called by the Swing runtime.

As was already pointed out, process is not called by your code -- it is called on your behalf after you call publish, and maybe not every time you call publish().

Does that clear things up for you?

like image 54
arcy Avatar answered Nov 01 '22 22:11

arcy


You should call publish() to add data to be processed by the process() method.

The publish method can be called from any thread.

The process method is executed within the EDT thread so you are allowed to change GUI stuff only allowed to do so from within the EDT thread, hence you should not call the process() method yourself.

It is up to you to decice what your process() must do, but remember it is running in the EDT thread so it should finish very quickly.

like image 3
GerritCap Avatar answered Nov 01 '22 21:11

GerritCap