• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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

 
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 14488
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 14488
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Yeah, but is it art? What do you think tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic