Advertisement
class Intr implements Runnable //implementing the Runnable interface
{
public void run() //Entry Point of the new thread.
{
try
{
Thread.sleep(3000); //This will make the thread(Thread2) sleep for 3000ms.
}
catch(InterruptedException e)
{
System.out.println("Thread is interrupted - " + e);
}
}
public static void main(String... ar)
{
Intr newTh= new Intr();
Thread th= new Thread(newTh, "Thread2"); //Calling Thread's constructor & passing the object
//of class that implemented Runnable interface
//& the name of new thread.
//Starts the thread(Thread2)
th.start();
//Main thread interrupting the thread(Thread2) while it is sleeping, causing InterruptedException
th.interrupt();
}
}
Thread is interrupted - java.lang.InterruptedException: sleep interrupted
Advertisement
class Intr
{
public static void main(String... ar)
{
//Accessing the reference of main thread, a default thread
Thread th = Thread.currentThread();
//Interrupting the main thread.
th.interrupt();
try
{
Thread.sleep(3000);
}
catch(Exception e)
{
System.out.println(e + " in "+ th.getName()); //Printing the name of main thread.
}
}
}
java.lang.InterruptedException: sleep interrupted in main
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement