views:

847

answers:

4

Hiya,

My question pertains to multi-threading in Java. I'm translating an app I wrote in Visual Basic 2008 into Java. There is a class in VB called BackgroundWorker, which allows the coder to perform a task on another thread, a lot like SwingWorker in Java. The only distinct difference is that, with the BackgroundWorker thread is run(), it fires an event called DoWork() on the mainline which contains the code to execute in the background. Furthermore, after the code has executed, a RunWorkerCompleted() event is fired back on the foreground thread to interpret results.

I have found the BackgroundWorker setup quite useful and it seems a little more flexible than SwingWorker and I was just wondering whether it was possible (and acceptable) to fire events in the same way in Java? And if so, how would I go about it? Since I've only done a quick scan over SwingWorker, it's possible that it has a similar functionality that would work just as well, in which case I would be happy to know about that instead.

Cheers,

Hoopla

+1  A: 

What was the mainline again?

Let me see if I get this right.

The background thread fires an event the GUI can intercept to know the data is ready.

Is that correct?

You may use SwingUtilities.invokeLater( Runnable r );

It inserts that runnable instance in the AWT event dispatch thread ( that I guess is the same as the mainline ) after all the events currently in that thread are processed.

This way you can fire an "actionPerformed" event and the registered listener will handle properly.

Let me code something really quickly and you tell me if this was what you wanted.

In the mean time take a look at here

OscarRyz
A: 

Not exactly sure if this will help , but looking at the SwingWorker from Java 6 (which is new and different from the previous releases), and the Java Tutorials on the matter, it may offer something similar to what you may be after.

Taking a look at the Concurrency in Swing section of The Java Tutorials, it has a section on Bound Properties and Status Methods which mentions a way of registering a PropertyChangeListener to the SwingWorker. This way, any events where you want to notify another thread can be performed by creating PropertyChangeEvents through the firePropertyChange methods.

Also, see the section on SwingWorker Is Now Included section of the More Enhancements in Java SE 6 article for a quick description of the above.

So, if I understand what the articles correctly, first, implement a PropertyListener on the thread where the messages from the SwingWorker thread should be sent to. Then, to send a message to back to the original thread, firing property changes fired from the SwingWorker in its doInBackground() (using the firePropertyChange method) could be used as means of sending messages to another.

coobird
Yeap that look promising
OscarRyz
A: 

The solely intention of the following code is to show how does it looks like when the SwingUtilities.invokeLater method is used.

The effect is the task is executed in the AWT-Event thread ( the one responsible for component painting ) .

The rest ( creating a new thread , creating a gui etc. ) is just scaffold code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class InvokeLaterEffect {

    static JTextArea text = new JTextArea();


    // See the diff by commenting.
    static void done() {
        SwingUtilities.invokeLater( doneAction ); 
        //doneAction.run();
    }


    public static void main( String [] args ) {
        JFrame frame = new JFrame();
        frame.add( text );
        frame.pack();
        frame.setVisible( true );

        bacgroundTask.start();
    }
    // run a task in the background
    static Thread bacgroundTask = new Thread(){
        public void run(){
            try { 
                System.out.println( Thread.currentThread().getName() + " Started background task ");
                Thread.sleep( 5000 );
                System.out.println( Thread.currentThread().getName() + " Finished background task");
                done();
            } catch ( InterruptedException ie ){}
        }
    };

    // called whtn id done
    static Runnable doneAction = new Runnable(){
        public void run(){
            System.out.println( Thread.currentThread().getName() + " start setting text ");
            text.setText("Hello");
            System.out.println( Thread.currentThread().getName() + " finish setting text ");
        }
    };


}

The output is like:

Thread-2 Started background task
Thread-2 Finished background task
AWT-EventQueue-0 start setting text
AWT-EventQueue-0 finish setting text
OscarRyz
A: 

Kewl. Cheers guys, thanks for the prompt replies, and apologies for my rather time-lax reply. I'll give your idea a go Oscar, sorry coobird, I didn't quite follow - any further explanation would be welcome (perhaps an example).

Just to recap: I want to have a runnable class, that I instantiate an instance of in my code. The runnable class has two events, one of which is fired from the background thread and contains the code to run in the background (DoWork()), and the other event is fired on the foreground thread after the background thread has completed it's task (RunWorkerCompleted()).

And if I understand your advice correctly, I can fire the DoWork() event from the runnable class' run() method so that it will be executed on the background thread, and then I can use the SwingUtilities.invokeLater() method to fire the RunWorkerCompleted() event on the foreground thread, once the background thread has finished execution.

Yes?

Hoopla