Category Archives: thread

Using SwingWorker to update Swing Components asynchronously

I have a JList, the contents of which are updated asynchronously by a different thread. The way I update the JList is by updating the model:

The only problem is, if this is not done in the EventDispatchThread, the UI becomes unresponsive, and starts behaving weird.

The way to solve this to use the SwingWorker.

I have a Jini server, which streams data to a Jini client, in this case, a Swing JFrame. I have a JList to which I publish the data as and when it becomes available.

Jini Service API

Its a simple streaming service:

At its heart is the interface RemoteDataListener, on to which the server publishes data as it becomes available.

Client Implementation

The key is to implement the interface RemoteDataListener along with the SwingWorker as shown below as an inner class in the JFrame:

We invoke the long running service in the doInBackground() method and add itself as a listener. When new data is received in the newData() method, we immediately call the publish() method, which delegates it to the process() method. The process() method is invoked within the eventDispatcherThread. This ensures that any updates made to the model of the JList is reflected on the UI. So, the code to update Swing components reside here. Also note that the done() method is called by the SwingWorker after the processing thread is finished.

Running the example

Run the SpringNonSecureRmiServer to start the Reggie and the Jini Server. After the Jini Server starts up, run the RmiStreamingDemoFrame.

rmi-data-streaming-1

rmi-data-streaming-2

Sources

The sources can be found here: https://github.com/paawak/blog/tree/master/code/jini/unsecure/streaming-with-jini

There are 3 Maven projects under that:

  1. rm-api
  2. rmi-client
  3. rmi-server

CountDownLatch example

Click here to see the Applet in action.
Click here to see the Applet in action.

As shown above, the 4 progress bars start one after another after the start button is clicked.

The following code is for the JPanel with the 4 progress bars:

This is the Thread using the CountDownLatches to coordinate between each other:

Finally, this is the Applet code:

The sources can be found here: https://github.com/paawak/blog/tree/master/code/Demo

 

A simple wait notify example

At times we often need to fetch an object which might take a long time. Our preferred way of doing that, especially when we are on a UI thread, is to spawn a different thread so as to keep the UI responsive (this is just one of the many use cases that I can think of now). But since we need that object to proceed further in the current execution, we have to resort to some sort of wait/notify mechanism. The following code demoes a very simplistic approach using the regular wait()/notify().

Note: This is far from fool proof. One case where it will fail is if the long task is over before that lock.wait() is called.

Producer/Consumer

I have tried to come-up with a thread safe and a non thread safe version of a typical producer/consumer scenario.

Run this with

boolean THREAD_SAFE_EXECUTION = true;

and

boolean THREAD_SAFE_EXECUTION = false;

And see the difference yourself :).