views:

74

answers:

2

Usually one is looking for a way to update the UI while something in the background is still working. My approach there would be either use a new thread or simply use SwingUtilities.invokeLater.

However, right now I'm in the completely opposite situation. I have a button that calls a static method which does some magic (not important here). Pressing the button also starts an animation on the UI thread (currently implemented using a Swing Timer). When the magic method is finished, it first fires an event to the UI and then continues doing something else. The event tells the UI to stop the animation - after some delay (actually the UI is supposed to show a different animation for a short time).

That delay however is exactly the problem, because I want to prevent the magic method from continuing before the animation has finished. The event listener call is currently implemented synchronizely, so the event listener won't return before it completes. But as the animation is asynchronous, I don't know how to block the background process for that time. I thought I could use a ReentrantLock to make it synchronized, but when I tried to lock it from the UI thread, that lock was ignored.

What is the correct way to make the background process wait for the UI?

edit

The code below shows extremely simplified what happens. The UI is just one of the actual listeners (the only blocking one though), so I would rather have some blocking effect on the UI side of the event handling.

public class Worker
{
    public static void magicMethod()
    {
        // do something
        // ...

        // fire event
        for ( ExampleEventListener listener : listeners )
            listener.onExampleEvent( new ExampleEvent() );

        // BLOCK until animation in UI completed
        nextStep();
    }

    public static void nextStep ()
    {
        // continue work
    }
}


public class MyWindow extends JFrame implements ExampleEventListener
{
    public void actionPerformed ( ActionEvent event )
    {
        // button clicked
        Worker.magicMethod();
    }

    public void onExampleEvent ( ExampleEvent event )
    {
        startAsynchronousAnimation();
    }

    private void completeAnimation ()
    {
        // animation completed
        animationPanel.setVisible( false );

        // UNLOCK
    }
}
A: 

You could have your background process run in the Event Dispatch Thread - the same thread that Swing executes on. Then simply have your background code execute after a dialog prompt or whatever, and once that returns your background code will execute, effectively making it wait for the UI.

Shakedown
After checking that, it turns out that I actually everything is running on the *AWT-EventQueue* thread. The application starts with user input, which then calls the static class (which holds the "magic method"), which then again calls the event listeners (UI again). However my actual problem is that the animation is displayed asynchronous, and I don't want the animation to be blocked.
poke
+1  A: 

Not sure if its the best way, but you could use a shared boolean value to signal if the background process is ready to continue. Using wait() will cause the thread to release the lock an object in a synchronized block. Something like:

while(! ready){
    try{
        mySynchonizedObject.wait(); 
    }catch(InterruptedException ex){
        System.exit(1);
    }
}

Where 'ready' is a shared boolean value. When you UI is ready it would then set ready to true and call notifyAll().

DrDipshit
Is that possible without the background having a reference to the UI? Or would that also work if I do that in the event handler (inside of the UI object)?
poke
Sure, the background process could store the ready value. The UI would however need a reference to it to set the value. The UI would basically signal when its ready by setting the value and calling notifyAll.
DrDipshit
Okay, I tried this now and got a *IllegalMonitorStateException* on the `wait()`. What kind of object do I have to use for synchronization there?
poke
After I put a synchronize block around it, the exception disappeared, but the UI simply stopped responding (because of the wait), so the animation won't get displayed at all - what to do?
poke
calling wait() must be done while and object is in a synchronised block, as it releases the lock on the object. i had thought that was what you wantd from your description. you could put the thread to sleep instead.
DrDipshit
Just keep puttimg the thread to sleep until your ready variable is set using Thread.sleep(TIMEOUT). since you function is static the ready variable would have to be static too, or generated from a static context. that variable would need to be set by the ui somehow to break out of the loop.
DrDipshit
After multithreading the event listener calls, I was able to go very similar to this solution. Thanks.
poke