aspose file tools*
The moose likes Java in General and the fly likes How to report progress in executing body of a method to its invoker? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to report progress in executing body of a method to its invoker?" Watch "How to report progress in executing body of a method to its invoker?" New topic
Author

How to report progress in executing body of a method to its invoker?

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Hi

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....

Is there anyway to do that?

Thanks.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

No, because the calling and the called method share the same thread. If you want to report progress, you should let your method update some variable which is then read by another thread.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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.

rc
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

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?
raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
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.

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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:




I hope this is enough for you to get the idea.

rc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to report progress in executing body of a method to its invoker?