Muhammad Danish
Muhammad Danish

Reputation: 109

When InterruptedException thrown from Thread.sleep?

when does the thread.sleep generate InterruptedException? I am using this but exception not occuring.Please help about using threads. about passing information to thread and returning information from thread. public class myThread extends Thread{

@Override
public void run(){

    try{

    while(true) {
        Thread.sleep(200);   
        System.out.println("Thread is running");

    }
   }
   catch(InterruptedException ex){
   System.out.println("Exception Occur");
   }
}

exception does not occur after 200ms. why??

Upvotes: 1

Views: 3535

Answers (2)

Marko Topolnik
Marko Topolnik

Reputation: 200158

InterruptedException is thrown in response to Thread.interrupt being invoked on the Thread instance managing the thread in which you call sleep. You can easily test it by calling that method yourself, even within the same thread where you call sleep. Just precede that call with Thread.currentThread().interrupt() and see what happens.

Upvotes: 4

Mihail Burduja
Mihail Burduja

Reputation: 3256

As I know, InterruptedException is generated when you try to kill/interrupt the thread that is sleeping.

Upvotes: 4

Related Questions