Is there a way for a thread to determine which thread interrupted it?

Is there a way for a thread to determine which thread interrupted it?

For instance:.

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

Thread shows nothing.

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

EDIT:

This is not the message or event mechanism I'm looking for.

but, it seems very crude. a way to get the class type, group of threads, or maybe just the priority of interrupting the thread will have some information to work with.

A typical use is to shut down the system - interrupting them all to get them out of their locking methods, and there is no other that I can think of now.

+6
source share
4 answers

Is there a way for a thread to determine which thread interrupted it?

In a word: None.

These standard Java SE class libraries do not have functionality.

Indeed, the problem is not even defined:

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

  • What about race conditions between a thread that detects that it has been interrupted (for example, via an isInterrupted call) and find out what the thread is?

As Sotirios said: if the signal flow needs to find out which flow signaled this, then interrupt is probably the wrong mechanism. You have probably created your own event mechanism in which events carry the necessary information with them.

+4
source

Before interrupting a stream, save the necessary information somewhere where the interrupted stream can receive it. Then, when the stream is interrupted, check this information wherever you saved it.

+2
source

You cannot say in the standard topic and, as already mentioned, if you need it, then the queue will be more useful.

In this case, a better solution would be to change the state.

eg.

 // change state in a thread safe manner sharedObject.setState(Mode.ACTION); thread.interrupt(); // doesn't need to know about other threads, just state changes. if (isInterrupted()) { switch(sharedObject.getState()) { case ACTION: } } 

In general, you can enter tasks to start a thread.

  // calling thread needs an action to perform. sharedThread.execute(runnable); // or invokeLater() // polling thread, use take() if you want to block. for(Runnable run; (run = runQueue.poll()) != null;) run.run(); 

However, this does not mean that it is impossible to do this just because it is probably not a good idea.

 public class Main { static class MyThread extends Thread { protected final Queue<Thread> interruptingThreads = new ConcurrentLinkedQueue<>(); MyThread(Runnable target) { super(target); } public Queue<Thread> getInterruptingThreads() { return interruptingThreads; } @Override public void interrupt() { interruptingThreads.add(Thread.currentThread()); super.interrupt(); } } public static void main(String... ignored) throws Exception { Thread t = new MyThread(new Runnable() { @Override public void run() { try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); System.err.println("Interrupted by :" + ((MyThread) Thread.currentThread()).getInterruptingThreads()); } } }); t.start(); Thread.sleep(500); t.interrupt(); } } 

prints

 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at Main$1.run(Main.java:53) at java.lang.Thread.run(Thread.java:745) Interrupted by :[Thread[main,5,]] 

If this is debugging, you can also add a stack trace where the interrupt thread called interrupt()

+1
source

If you just need to know the interrupt thread for debugging, and not in the code itself, I found the following message useful, which describes adding a new SecurityManager: Is there any way in Java to register * every * interrupt thread?

0
source

Source: https://habr.com/ru/post/973246/


All Articles