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:
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...
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 ?
[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.
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:
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?
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.
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?