Alex Rivera | Logout

How to make a thread sleep for specific amount of time in java?

Asked 2010-09-26T13:20:16.907
14

I have a scenario where i want a thread to sleep for specific amount of time.

Code:

    public void run(){
        try{
            //do something
                     Thread.sleep(3000);
//do something after waking up
                }catch(InterruptedException e){
                // interrupted exception hit before the sleep time is completed.so how do i make my thread sleep for exactly 3 seconds?
                }
        }

Now how do i handle the case where the thread i am trying to run is hit with an interrupted exception before the complete of the sleep? Also does the thread wake up after being interrupted and does it go to runnable state or when is it that only after it goes to runnable does the flow go to catch block?

Edit
Report

1 Answer

1

Why do you want to sleep for exactly 3 seconds? If it's just having to execute something after some time, try using a Timer.

answered 2010-09-26T14:14:35.073

Your Answer