aspose file tools*
The moose likes Threads and Synchronization and the fly likes How to kill an slepping thread ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to kill an slepping thread ?" Watch "How to kill an slepping thread ?" New topic
Author

How to kill an slepping thread ?

Eusebio Floriano
Ranch Hand

Joined: Mar 07, 2004
Posts: 237
Hi folks,
I�m having the following problem:
A thread that i started always cause dead lock wainting for a resource that it will never get. I just want to kill it, but I don�t know how to do it.
After start this thread, i�ve already set up a timeout in the method join(), with no sucessful. Here the snippet:

How can I finish this thread ?

Regards,


SCJP 1.4 / 5.0 - SCBCD 1.3 - SCWCD 1.4 - IBM 484
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It would depend on what exactly your run() method is doing, I suppose. If it's really sleeping (as in, it's calling Thread.sleep()) then perhaps you should call t.interrupt(). This will cause the sleep() to throw an InterruptedException, and if you position the catch block at teh end of your run() method, calling t.interrupt() will cause the thread to move from sleep() to the catch block, and thus to the end of the run() method. It's hard to say any more without seeing more of the code inside run().

However - why are you using a separate thread here anyway? Your code is starting a separate thread, and then immediately calling t.join(). That completely nullifies any benefit you might get from having a second thread. It's like getting two people to work together to finish a job, except that the first person stops working when the second one shows up, and waits until the second person finishes his part and leaves. Why bother? Just let the first person (or the main thread) do all the work itself then. Unless there's more to this problem than you've revealed...


"I'm not back." - Bill Harding, Twister
Eusebio Floriano
Ranch Hand

Joined: Mar 07, 2004
Posts: 237
Hi Jim,
Thx for the help.
Actually, the thread gets blocked, not asleep.
In my method run() what i have to do always blocks the thread and it never ever ends.
So, my idea was to create a new thread and after this thread be blocked just kill it anyway, so my main thread would not be blocked.
I�m not experience in threads and i tought that if i created that thread and then just call the method join(), after the timeout, the thread would be automaticly terminated, but it continues alive, doesn�t it ?

Regards,
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Vinicus]: Actually, the thread gets blocked, not asleep.
In my method run() what i have to do always blocks the thread and it never ever ends.


OK. Unfortunatle it's not really possible to suggest good alternatives here unless we know what the thread is doing (what method is it executing) when it's blocking.

So, my idea was to create a new thread and after this thread be blocked just kill it anyway, so my main thread would not be blocked.

Well, it's possible this idea may still be useful, but again, it really depends on wht the blocked thread is doing.

I�m not experience in threads and i tought that if i created that thread and then just call the method join(), after the timeout, the thread would be automaticly terminated, but it continues alive, doesn�t it ?

Yes - if the thread you called join() with has not ended by the time join() times out, then join() simply returns, with the thread still alive. (You can test if the thread is still alive with t.isAlive().) Using join() probably won't help you for your problem.
Eusebio Floriano
Ranch Hand

Joined: Mar 07, 2004
Posts: 237
I�m trying to implements a programm to reset my router.
So i create an URLConnection passing the URL for resetting the router and then submit the request.
The problem is that the thread gets blocked waiting for the response that will never be returned.
The solution that i thought was the thread that created this blocked one just kill it.
Here the complete code:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think the best solution for your problem would be to use JDK 1.5, which introduces a setReadTimeout() method which will solve your problem nicely.

If you can't use 1.5 for your project, then you might try calling close() on the URLConnection from another thread. Sometimes this works; in other cases it simply causes close() to block indefinitely. You might also try calling interrupt() on the thread as well. It probably won't work, but it might - you never know. If nothing else works, you might have to use NIO to create a SocketChannel and set it to nonblocking mode. Then you'll probably have a lot more work figuring out how to implement the full HTTP protocol using this channel - unless someone's already done this somewhere; has anyone seen something like this?
Eusebio Floriano
Ranch Hand

Joined: Mar 07, 2004
Posts: 237
Hi Jim,
Thx a lot for your help.
The method setReadTimeout() just worked fine and it unblocks the thread.
I was trying setConnectTimeout() with no success.
And as you said, trying to use interrupt() does not work.

I really really appreciate your help.

Regards,
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

An old trick of mine is to close the connection which will unblock the thread. This requires another thread.
Eusebio Floriano
Ranch Hand

Joined: Mar 07, 2004
Posts: 237
Which class you call close() on ?
URLConnection does not have this method.

Regards,
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hunh - I guess it doesn't. Most other classes with a name like XXXConnection have a close() method - I (and evidently CLG) figured this fit the same pattern. Oh well. It's a good thing you can use 1.5 here then, isn't it?
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Vinicius Boson:
Which class you call close() on ?
URLConnection does not have this method.

Regards,


oops. Let me walk out onto an OOP limb here. The class you called open() on?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to kill an slepping thread ?