Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need sample program to throw InterruptedException

I am going through the kathy sierra SCJP 1.5 Chapter 9(threads) and there it is mentioned as:

Notice that the sleep() method can throw a checked InterruptedException (you'll usually know if that is a possibility, since another thread has to explicitly do the interrupting), so you must acknowledge the exception with a handle or declare

I just need a sample program to know when it happens (which i can run on my machine)?

I googled but could not find any sample code to test this functionality..

Thanks in Advance

like image 428
javanoob Avatar asked Sep 04 '10 12:09

javanoob


People also ask

How do you throw an InterruptedException?

There are several methods in Java that throw InterruptedException. These include Thread. sleep(), Thread. join(), the wait() method of the Object class, and put() and take() methods of BlockingQueue, to name a few.

What is InterruptedException explain it with a small example?

InterruptedException ) is a checked exception [6] which directly extends java. lang. Exception . This exception is thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity [7]. If an InterruptedException is caught it means that the Thread.

Can Yield throw an InterruptedException?

yield method does not throw Interrupted Exception.

Why InterruptedException should not be ignored?

InterruptedExceptions should never be ignored in the code, and simply logging the exception counts in this case as "ignoring". The throwing of the InterruptedException clears the interrupted state of the Thread, so if the exception is not handled properly the information that the thread was interrupted will be lost.


1 Answers

Here's an example:

public class Test
{
    public static void main (String[] args)
    {
        final Thread mainThread = Thread.currentThread();

        Thread interruptingThread = new Thread(new Runnable() {
            @Override public void run() {
                // Let the main thread start to sleep
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                mainThread.interrupt();
            }
        });

        interruptingThread.start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            System.out.println("I was interrupted!");
        }
    }
}

To walk through it:

  • Set up a new thread which will sleep for a short time, then interrupt the main thread
  • Start that new thread
  • Sleep for a long-ish time (in the main thread)
  • Print out a diagnostic method when we're interrupted (again, in the main thread)

The sleep in the main thread isn't strictly necessary, but it means that the main thread does get to really start sleeping before it's interrupted.

like image 175
Jon Skeet Avatar answered Oct 05 '22 18:10

Jon Skeet