2

I have run into a weird dependency when trying to cancel an anonymous SwingWorker. My current code:

private static void init() {
    if (connected) {
        return;
    }
    //final SwingWorker<Void, Void> initWorker;
    final Timer noConnectionTimer = new Timer(5000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //initWorker.cancel(true);
            waitObject.setTimedOut(true);
            connected = false;
            waitObject.process();
        }
    });
    new SwingWorker<Void, Void>() {
        @Override
        public Void doInBackground() {
            noConnectionTimer.setRepeats(false);
            noConnectionTimer.start();
            cachedInstance = new Network(Config.HOST_NAME, Config.HOST_PORT);
            if (connected) {
                noConnectionTimer.stop();
                new Thread(cachedInstance).start();
                waitObject.setTimedOut(false);
                waitObject.process();
            }
            return null;
        }
    }.execute();        
}

First assumption I want to be verified:

I can kill the new Network(Config.HOST_NAME, Config.HOST_PORT) code by killing (cancelling) the SwingWorker.

So assuming my assumption is correct, I want to cancel the anonymous SwingWorker, but when I try to give it a name initWorker, then it needs to be final such that the timer can reference it. However the SwingWorker itself needs to reference the timer aswell, so the timer also needs to be final.

So I think I managed to create a dependancy between two final variables that need eachother, how can I fix this?

Regards.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
skiwi
  • 66,971
  • 31
  • 131
  • 216
  • What do you mean by "I can kill the new Network(...) code". What does "killing code" mean? And what's the point of starting a Timer from a background thread? Starting a Timer is not a long running task. You can do it from the EDT. – JB Nizet May 18 '13 at 20:48
  • @JBNizet The Socket() constructor blocks until a connection has been found (or platform dependent timeout ~1 min). However if after 5 seconds there is no connection yet, then I want to stop the code after the Socket() constructor from being executed. So basically if you still manage to connect anywhere between > 5s and < ~1 min, then currently on the background it still connects, while it shows the user (and sets variables) as if it is not connected. – skiwi May 18 '13 at 20:52
  • Read the javadoc of cancel(). It explains it. You must pass true as argument (mayInterrupt) and check if the current thread is interrupted right after the Network constructor returns. If the thread is interrupted, then close the socket and return. – JB Nizet May 18 '13 at 21:07
  • hmmm SwingWorker is ...., there are 5-8 methods that I missing, [have to naming each of threads, otherwise you can to lost in their instancies, add PropertyChangeListener, add ..., so on](http://stackoverflow.com/questions/7053865/cant-get-arrayindexoutofboundsexception-from-future-and-swingworker-if-threa) – mKorbel May 19 '13 at 05:05

1 Answers1

1

You don't have to add an ActionListener upon creation. You could just pass null to the Timer's constructor and add the ActionListener afterwards, using addActionListener.

Lone nebula
  • 4,768
  • 2
  • 16
  • 16
  • Removed part of answer, as [javax.swing.Timer](http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html) seems to be thread safe. – Lone nebula May 18 '13 at 21:58