Kubuntu
Kubuntu

Reputation: 11

How can I resume a thread which is waiting for a resource?

How can I interrupt/resume a resource waiting thread back to other works? Say, My thread is waiting for response from a resource where the resource hanged or not in the state of response. How can I resume back that thread to do other works?

Can other thread send a exception to the waiting thread, so that with an exception, it can come back to it's other work?

UPDATE :

I have tried it in the following way, but no use.

I have timeout Thread, which will check whether default timeout has occurred to that particular Transaction on which the first thread wait, and if so, fire interrupt() call on the first thread.

Upvotes: 1

Views: 141

Answers (2)

yurib
yurib

Reputation: 8147

yes, you can interrupt the thread. using the Thread.interrupt mechanism, google it.

Upvotes: 0

Eyal Schneider
Eyal Schneider

Reputation: 22446

The answer depends on many factors. The "right" way to interrupt a thread is to use the thread.interrupt() method. This requires the thread's code to be cooperative; it should be aware of the interruption, either by handling the InterruptedException thrown by interruptible operations (such as wait or sleep), or by checking the interrupted flag of the current thread regularly.

Then, the code should decide what consequences the interruption should have. Usually, you would like a thread to terminate gracefully in case of interruption.

Instead of using interruption, I would check whether your blocking operation has a built in timeout mechanism. If it's blocked on obj.wait() for example, then you could use obj.wait(timeout) instead.

Upvotes: 3

Related Questions