I found one question on similar lines : Interrupt with name identifier [Sigaction - Linux]
- How to identify name or any contextual info of thread A when it interrupts thread B?
- Even if we get name of thread A, there is chance there could be multiple threads with same name as mentioned here : https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#interrupt--
Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it. - Does this mean I would have to create custom thread inheriting "java.lang.Thread"?
Code Sample
Thread threadA = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException ex) { }
System.out.println("I am going to interrupt, catch me if you can");
threadB.interrupt();
}
});
Thread threadB = new Thread(new Runnable() {
public void run() {
try {
System.out.println("threadB going to be interrupted");
Thread.sleep(5000);
} catch (InterruptedException e) {
//How to find name or any contextual info of threadA here?
Thread.currentThread().interrupt();
}
}
});
threadB.start();
threadA.start();
If this is not possible directly, is there any workaround or trick?