Finding out which thread has interrupted

M

Manish Jethani

Thread th = Thread.currentThread();

try {
th.sleep(10000);

} catch (InterruptedException e) {
/* I want to know *which* thread interrupted this one. */
}

How can I achieve this?

Tnx.

Manish
 
J

Joona I Palaste

Manish Jethani said:
Thread th = Thread.currentThread();
try {
th.sleep(10000);
} catch (InterruptedException e) {
/* I want to know *which* thread interrupted this one. */
}
How can I achieve this?

I think it's pretty much impossible directly. The only way is to have a
"setInterruptingThread(Thread t)" method in your thread, and then call
it from the same thread that interrupts it.
 
F

FISH

Manish Jethani said:
Thread th = Thread.currentThread();

try {
th.sleep(10000);

} catch (InterruptedException e) {
/* I want to know *which* thread interrupted this one. */
}

How can I achieve this?

Tnx.

Manish


Create a reference somewhere which the interrupting thread assigns to
itself immediately before requesting the interrupt. Make sure you cater
for when two threads attempt to interrupt the same sleeping thread at
the same time. Either queue them, or discard all attempts after the
initial one, depending upon your needs.


-FISH- ><>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top