Search code examples
javajava.util.concurrent

What or who should interrupt a Thread?


According to Goetz in his book JCIP :

Because each thread has its own interruption policy , you should not interrupt a thread unless you know what interruption means to that thread .

Why did the Java language provide a public interrupt () method then ?Is this a design flaw? Who or what is supposed to interrupt a thread then ?


Solution

  • What he means is that if you don't know what a thread does and how it works, you should not interrupt it. Since all threads can be interrupted, it is logical to have an interrupt() method in class Thread.

    You could ask the same thing for many other methods that can do "harm" when executed at the wrong place. The methods are the tools, and the programmer must use these tools wisely in order to create a program that works correctly.