• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Timeout functionality needed for process

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written an application that executes an Ant script. Unfortunately, sometimes the Ant scripts exits unexpectedly if something is incorrect in the Ant script. My program doesn't pick this exit up and hangs forever. I was thinking of putting a inactivity timeout value for the process or InputStream. Does anyone know how to implement this or any ideas to handle this situation? Any suggestions would be greatly appreciated. Thanks.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My best advice would be that you should get rid of those "incorrectness" that suddenly happen in your Ant scripts and that make your application hang.

That would be curing the disease, not simply soothing the pain.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree it would be better to cure the disease than put a bandaid on it (mixed metaphor warning!) but if you cannot ...

That will cause your main thread to throw an exception if the real work doesn't finish soon. It won't kill the real work thread. You can try to do that separately or just let it hang for a while.
[ May 31, 2006: Message edited by: Stan James ]
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the input. I absolutely agree that the Ant code needs to be working but I am trying to build in safeguards in case there is a problem with the Ant script that it doesn't make the thread hang indefinitely. Does the join method detect the underlaying process status? The thread continues to run and does not detect the process exit because the process does not issue an exit value. I was thinking that maybe I can monitor the InputStream. If it stops for a given amount of time, then it would destoy the thread. Is there a good way to monitor the InputStream to see if it stops writing?
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the problem with the simple InputStream is that it won't interrupt if the thread gets blocked by a reading or writing operation, even if you explicitly interrupt the hanging thread by calling its interrupt() method.

Then, instead of using a simple InputStream, maybe, you should use a FileChannel:

See java.nio.channels.FileChannel

Since FileChannels implement InterruptibleChannel, your thread may be interrupted even while it is blocked by an IO operation like a read or a write.

Then, as Stan suggested, you could ask your main thread to join your "IO thread" for a period of time that you consider reasonable, if when the join operation ends, the thread doing the IO operation is not yet terminated,that's to say if...

!theThread.getState()==Thread.State.TEMINATED

...then you can ask the hanging thread to interrupt:

theThread.inturrpt()

Be sure to handle ClosedByInterruptException in the run() method of your "IO thread" so that you can do some cleanup when the IO operation is interrupted.

Does this sound useful?
[ May 31, 2006: Message edited by: Edwin Dalorzo ]
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds very useful. Thanks so much replying. I will give it a try. Thanks..
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean somewhat like this:



Now, if you use join(timeout) it may imply that the main thread would have to wait some time even after the IO thread has finished. To improve that you may put your main thread to sleep for a smaller period and check the IO threads state, if after a number of checks the IO thread has not succeed it, interrupt it.


[ May 31, 2006: Message edited by: Edwin Dalorzo ]
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edwin, Let me thank you again for taking the time to reply. I see that this method works under the premis that one knows how long an Ant build, or any spawned process, is to take. If it takes longer than a given time, it "times out" and interupts the thread. I could probably do this since I know how long a typical build is to take. I was initally thinking of timing inactivity in the IO. Say there is a minute or two of total inactivity then it would call the interupt. That way it is universal no matter how long the process would normally last. I don't know if that is possible and I may be wanting too much.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set MAX_SECONDS to the maximum time you would like to wait for every IO operation, and before every read or write operation reset the current counter to 0.

That way the main thread will interrupt the IO thread only if a single IO operation exceeds the timeout, and not if the whole file processing exceeds the timeout.

I have to admit that it sounds like something I would do just to patch the code, and I wouldn't feel very proud of it. But the whole question, all along, has been about patching a piece of code, hasn't it?

What do you think?
[ May 31, 2006: Message edited by: Edwin Dalorzo ]
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds like it would work. I will do it as you say as a patch but will try to think of a more eloquent solution down the road. Thanks again for the input..
 
reply
    Bookmark Topic Watch Topic
  • New Topic