How to manually generate Interrupted Exception in Threads : Java Program Code

When someone writing a test program and one need to pause for some amount of time, so one call Thread.sleep(). But then the compiler or IDE balks that you haven't dealt with the checked InterruptedException. What is InterruptedException, and why do you have to deal with it?

The most common response to InterruptedException is to swallow it -- catch it and do nothing .

 The interruption mechanism provided by Thread and supported by Thread.sleep() and Object.wait() is a cancellation mechanism; it allows one thread to request that another thread stop what it is doing early. When a method throws InterruptedException, it is telling you that if the thread executing the method is interrupted, it will make an attempt to stop what it is doing and return early and indicate its early return by throwing InterruptedException.

Read Also :     Threads LifeCycle along with Example

Thread interruption

Every thread has a Boolean property associated with it that represents its interrupted status. The interrupted status is initially false; when a thread is interrupted by some other thread through a call to Thread.interrupt(), one of two things happens. If that thread is executing a low-level interruptible blocking method like Thread.sleep(), Thread.join(), or Object.wait(), it unblocks and throws InterruptedException. Otherwise, interrupt() merely sets the thread's interruption status. Code running in the interrupted thread can later poll the interrupted status to see if it has been requested to stop what it is doing; the interrupted status can be read with Thread.isInterrupted() and can be read and cleared in a single operation with the poorly named Thread.interrupted().




Here in this code  we are trying to create a System generated InterruptedException 

interrupted exception in java source code




















Please find  the code  below :



public class Start
{
    public static void main(String args[])
    {
        
        yes y=new yes();
        y.start();
    }
    
}

class Ie extends Thread
{
    
    public Ie()
    {
        super("Ie");
    }
    
    public void run()
    {
        for(int i=0;i<2;i++)
        {
            System.out.print(Thread.currentThread().getName());
            try
            {
                Thread.sleep(2000);
            }
            catch(InterruptedException e)
            {
                System.out.println("Interrupted Exception");
            }
        }
    }
}

class yes extends Thread

{
    public yes()
    {
        super("yes");
    }
    
    public void run()
    {
        for(int i=0;i<5;i++)
        
        {
            System.out.print(Thread.currentThread().getName());
            Ie t1=new Ie();
            
            t1.start();
            t1.interrupt();
            
        }
    }
}

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry