1

in a method, I need to call some code but after the method's return call. How do I do this?

// this call needs to happen after the return true call  
xmlRpcClient.invoke("newDevices", listDeviceDesc);

return true;
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
user840930
  • 5,214
  • 21
  • 65
  • 94
  • invoke it after the method returning `true` is invoked... – siledh Nov 07 '13 at 16:01
  • Can You tell us what u wanna execute after return call? – Govan Nov 07 '13 at 16:03
  • @Govan, he's talking about the `xmlRpcClient.invoke` line – jonhopkins Nov 07 '13 at 16:05
  • Can you please clarify this question? What are you trying to do? Why do you need to call invoke after returning? Finally, there's really no way to call a method in one method after the second one has returned! If you think about it, it just doesn't make sense. – Giovanni Botta Nov 07 '13 at 16:09

4 Answers4

5

Like JohnHopkins said use try{return true;}finally{yourCode} to execute to code after the return was called. But IMHO this isn't properly thought through and I would change the design of the program. Can you tell us a bit more about your thought behind this to understand your way?

What you probably want to do:

public void myMethod() {
  return true;
}

if(myMethod()) {
  client.invoke()
}
Lukas Eichler
  • 5,689
  • 1
  • 24
  • 43
  • The method is in a handler for a servlet, so that is why I can't really leave the method, but it all has to happen in the method. – user840930 Nov 08 '13 at 15:44
1

You could use an anonymous Thread to achieve what you want and add an one second delay inside.

try{return true;}finally{yourCode} will not do the trick, since finally will be executed before the method actually returns.

new Thread() {

    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // this call needs to happen after the return true call  
        xmlRpcClient.invoke("newDevices", listDeviceDesc);
    }
}.start();
return true;
Panagiotis
  • 401
  • 4
  • 13
0

I was reading up on the finally block in Java, and I learned that it will always be executed, unless the JVM crashes or System.exit() is called. Some more information can be found in this StackOverflow question. Given this information, this should work for you.

try {
    return true;
} catch (Exception e) {
    // do something here to deal with anything
    // that somehow goes wrong just returning true
} finally {
    xmlRpcClient.invoke("newDevices", listDeviceDesc);
}
Community
  • 1
  • 1
jonhopkins
  • 3,844
  • 3
  • 27
  • 39
0

IMO, doing something "after the return was called" but before the calling method has processed the return value is indistinguishable from doing it before the return, so you should ask yourself, when exactly you want it to happen.

In a Swing GUI Application, you can use SwingUtilities.invokeLater to delay the execution of a runnable until "everything else" is done. This is sometimes useful when a single user action causes a lot of listeners to be executed (one component loses focus, another components gets it, and said other component is also activated... all with a single mouse click).

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102