I have a method which will execute a long running portion of code. I want to report back to the code that invoke my method the progress I am making in executing the code, for example to let the invoker know I am in pre-processing section, or processing, or validation....
Maybe not exactly to the called routine, but you can report progress to, say, the instance that invoked the called method, or to some other part of your code. I think this is probably what you meant anyway, since the next execution point of the calling method is sitting in the call stack, waiting for a return.
Pass a reference to a "listener" class to the call; as a simple example, perhaps you have a class called ProgressListener, implementing the method reportProgress(String progressString). The listener could be the same class as has the long-running call, and you could pass this as the instance you want to receive the progress reports.
Then have the called routine call reportProgress() with whatever string it wants the caller to know about.
This will work if all you are going to do is, say, display the string in the console while your code grinds away. Things can get much more elaborate if you want to estimate percentage of task done, or whatever. A String is a fairly crude way of reporting progress, is all I'm saying. And of course if THAT were all you wanted to do, the long-running method could do it itself.
But the fact that the caller and callee are in the same thread needn't keep you from using a listener.
Now, if you DO have a GUI, say in Swing, there may be restrictions about what you can or should do in a long-running calculation thread. Those considerations depend on whether you have a UI and which one you have, so I/we can't specify them here.
Stephan van Hulst
Joined: Sep 20, 2010
Hmm yes, I hadn't considered a listener. That would be a solution too, especially if you just want to print a string regarding the current status.
Maybe you can tell us a bit more about your program?
Joined: Jul 14, 2005
Thanks for the reply. I am looking to know how to implement a listener to get updates on the method progress. That particular class you mentioned is in Java EE while I am using swing.
Basically, I want to update the invoker which is client code (Client to my method, not a client server architecture) about the progress of the method. The invoker can either update a GUI, print the text to the console or whatever it wants to do.
Joined: May 29, 2005
I made up the class name I used; I'm sorry I hit on something that actually existed somewhere.
There is nothing magic about a listener. Let's assume you have a class Mandrake, and it calls a long-running method calculateWorld in a class called George: