• Post Reply Bookmark Topic Watch Topic
  • New Topic

Want timeout error on hung thread

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I make calls to partner web services with HttpURLConnection, which does not have a timeout parameter. Sometimes the partner accepts the request but never responds so my program hangs for a very long time, maybe forever. Is this a sensible way to spawn the communication onto another thread and make the main thread terminate? I don't think there is much I can do about the hung thread. It will eventually finish ... or not. Oh, the app runs in 1.3.

[ November 03, 2005: Message edited by: Stan James ]
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this is pretty much the technique that I use when an operation may not come back. Fire it off in another thread, and have the main thread only wait() for a specified period of time. There are other issues...

1. Although, it does not always work, it may be a good idea to interrupt the thread when a timeout occurs. The interrupted thread better be able to handle it though.

2. There should be some sort of "abort" mechanism. There is no need for the slow thread to continue, if the operation has been aborted.

3. There has to be a way to clean up after the operation too. It can get a bit hairy working out the different scenarios after an abort.

Henry
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I've used threads quite a bit in simple happy scenarios with good results, but haven't written much inter-thread signaling like wait & notify. It took me a bit of trial & error to get that going, but I think I grok it well enough to try it in my real system.

In this case the other thread is waiting on reading a response from a socket inside the HttpURLConnection. I haven't found any way to kill it. I will try to interrupt() and test for it just for good measure. It's too bad the language wasn't able to give us a clean way to kill another thread, but just defining "clean" makes it clear how hard that would be.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!