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.
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()
source share