6

I was trying to write a Producer Consumer model (Producer thread and Consumer Thread in java)

I was wondering how to handle the InterruptedException that is thrown by Thread.sleep method and the Object Class's wait() method

package producerconsumer;

import java.util.ArrayList;

public class Consumer implements Runnable {

    ArrayList<Integer> container;

    @Override
    public void run() {
        while(true){
        System.out.println("Consumer Thread Running");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(null==container){
            System.out.println("container is null");
        }
        //Removing values from Arraylist
        synchronized (container) {
            if(container.size()==0){
                try {
                    container.wait();
                } catch (InterruptedException e) {
                    //How to tackle this exception
                    e.printStackTrace();
                }
            }
            Integer removedInteger = container.remove(container.size()-1);
            System.out.println("removedInteger..."+removedInteger);
        }
        }
    }

    public void setContainer(ArrayList<Integer> container) {
        this.container = container;
    }
}

This is one example I have taken, In this example there may not be any need to take care of these exception (My Assumption).

But I wanted to know what could be the different scenarios possible in which we need to handle this exception.

Jason Braucht
  • 2,358
  • 19
  • 31
Sunny Gupta
  • 6,929
  • 15
  • 52
  • 80

1 Answers1

11

I was wondering how to handle the interrupted Exception that is thrown by Thread.sleep method and the Object Class's wait method

There are two important things to realize about InterruptedException. First off, when it is thrown the interrupt flag on the Thread is cleared. So you should always do something like:

} catch (InterruptedException e) {
    // re-add back in the interrupt bit
    Thread.currentThread().interrupt();
    ...
}

This is a very important pattern because it allows other code that might be calling yours to detect the interrupt as well.

Secondly, in terms of threads, if they are interrupted, they should most likely cleanup and quit. This is certainly up to you, the programmer, but the general behavior is to terminate and quit the operation being performed – often because the program is trying to shutdown.

} catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    // we are being interrupted so we should stop running
    return;
}

Lastly, with any exception handling, the default Eclipse template is usually the wrong thing to do. Never just e.printStackTrace(); the exception. Figure out what you want to do with it: re-throw it as another exception, log it somewhere better, or exit the thread/method/application.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • Another option is to just re-throw the exception (in which case you don't need to re-interrupt the thread). – yshavit Apr 25 '12 at 13:54
  • @Gray Thanks for putting it in an awesome manner. Just wanted to know if we re-throw this as another exception. Will my thread stop? or Will the loop carry on like nothing has happened. – Sunny Gupta Apr 25 '12 at 13:58
  • It's simple Java semantics. There's absolutely no magic. Your call to `sleep` or `wait` threw an exception. Execution continues just as if it were an `IOException` from, say, `InputStream.read`. – Marko Topolnik Apr 25 '12 at 14:02
  • As Marko points out @SAM, the `InterruptedException` is a normal `Exception`. If you catch it then execution of your thread continues. If you re-throw it as a `RuntimeException` and the loop does not catch that exception then the thread will return from `run()` and the thread will stop. Standard exception rules apply here. – Gray Apr 25 '12 at 14:18
  • My compiler says `wait()` never throws `InterruptedException`. Is that correct? Can I trust my compiler? ;)) –  Jan 16 '14 at 18:53
  • `Object.wait()` does throw `InterruptedException`. Maybe the method throws `Exception` or something that is masking it @Gracchus? – Gray Jan 16 '14 at 22:21