[concurrency-interest] enable / disable interrupt

Hontvari Jozsef hontvari3 at solware.com
Fri Mar 28 17:37:20 EDT 2008

I have read an elegant solution to the problem of interrupting a thread 
running third party code. The third party code may not respond in the 
expected way to the interrupt. One solution can be to run the third 
party code in a different thread, but this may make the code ugly and 
may double the count of threads. The proposed solution is to use a 
Thread subclass which overrides the interrupt method and adds functions 
to enable and disable interrupts.

It is in the book "Multithreaded Programming with JAVA Technology" by 
Bil Lewis; Daniel J. Berg:

public class InterruptibleThread extends Thread {
private boolean interruptsEnabled = false;
private boolean interruptPending = false;

public static void enableInterrupts() {
  InterruptibleThread self = InterruptibleThread.currentThread();

  synchronized (self) {
    self.interruptsEnabled = true;
    if (self.interruptPending) self.interrupt();
    self.interruptPending = false;

public static void disableInterrupts() {
  InterruptibleThread self = InterruptibleThread.currentThread();

  synchronized (self) {
    if (interrupted()) self.interruptPending = true;
    self.interruptsEnabled = false;

public synchronized void interrupt() {
  if (interruptsEnabled)
    interruptPending = true;

This book is relatively old, from 1999, but I don't find the idea 
implemented anywhere else. It seems to be so useful that if it does work 
then I would expect these functions to be included in the Java API 
already. But they aren't there of course. Is there something 
fundamentally wrong with this idea?

More information about the Concurrency-interest mailing list