I am in a wierd situation. In my web-server (tomcat), on web request, I basically need to cancel a previous request. I have a reference to the thread that was executing the previous request. So I can directly interrupt that thread and the node will do the rest.
I know you are not suppose to interrupt the thread which you do not own. But is it safe to interrupt tomcat thread in this case? What can be the other way? Maintaining own thread pool is waste of resources and ovehead
Maintaining your own thread pool is a waste of resource but it’s also a gain in every other respect, like stability of your application server. So you need to decide what is more important: A few thousand bytes of memory and CPU cycles or a stable, reliable application.
The problem with interrupting another thread is that you usually can’t know for sure where in the code that other thread is. You might want to use locking for this:
Thread A locks something while it’s safe to interrupt, thread B checks the lock and if it can’t get it, it interrupts A.
But what happens when A is just about to give up the lock, B checks the lock, A unlocks and starts with cleanup, B sends interrupt?
So you should really use your own thread pool.