Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way for a thread to tell which thread has interrupted it?

Is there a way for a thread to tell which thread has interrupted it?

Eg.:

...
if (isInterrupted())  {
    // look-up the thread that interrupted this
        // act accordingly
}

Thread isn't showing anything.

//========================

EDIT:

It isn't a message or event mechanism i'm looking for.

but, this as is seems very raw. a way to get the class type, thread group or maybe just the priority of the interrupting thread would carry some info to work on.

typical use is system shut-down-- interrupting them all to get them out of their blocking methods, and there isn't another i can think of now.

like image 222
user3401132 Avatar asked Aug 02 '14 01:08

user3401132


1 Answers

Is there a way for a thread to tell which thread has interrupted it?

In a word: No.

There is no functionality in that standard Java SE class libraries that support this.

Indeed, even the problem is not well defined:

  • What happens if a thread is interrupted multiple times by different threads? Which one should be reported?

  • What about race conditions between a thread detecting that it has been interrupted (e.g. via a isInterrupted call) and finding out what thread did it?

As Sotirios commented: if the signalled thread needs to find out which thread signalled it, then interrupt is probably the wrong mechanism. You probably to build your own event mechanism in which the events carry the information you need with them.

like image 118
Stephen C Avatar answered Oct 12 '22 17:10

Stephen C