I get exception when using Thread.sleep(x) or wait

2019-01-01 17:02发布

I have tried to delay - or put to sleep - my Java program, but an error occurs.

I'm unable to use Thread.sleep(x) or wait(). The same error message appears:

unreported exception java.lang.InterruptedException; must be caught or declared to be thrown.

Is there any step required before using the Thread.sleep() or wait() methods?

标签: java sleep
13条回答
浪荡孟婆
2楼-- · 2019-01-01 17:07

You have a lot of reading ahead of you. From compiler errors through exception handling, threading and thread interruptions. But this will do what you want:

try {
    Thread.sleep(1000);                 //1000 milliseconds is one second.
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}
查看更多
看淡一切
3楼-- · 2019-01-01 17:07

A simpler way to wait is to use System.currentTimeMillis(), which returns the number of milliseconds since midnight on January 1, 1970 UTC. For example, to wait 5 seconds:

public static void main(String[] args) {
    //some code
    long original = System.currentTimeMillis();
    while (true) {
        if (System.currentTimeMillis - original >= 5000) {
            break;
        }
    }
    //more code after waiting
}

This way, you don't have to muck about with threads and exceptions. Hope this helps!

查看更多
荒废的爱情
4楼-- · 2019-01-01 17:15

When using Android (the only time when I use Java) I would recommend using a handler instead putting the thread to sleep.

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.i(TAG, "I've waited for two hole seconds to show this!");

        }
    }, 2000);

Reference: http://developer.android.com/reference/android/os/Handler.html

查看更多
宁负流年不负卿
5楼-- · 2019-01-01 17:15

Alternatively, if you don't want to deal with threads, try this method:

public static void pause(int seconds){
    Date start = new Date();
    Date end = new Date();
    while(end.getTime() - start.getTime() < seconds * 1000){
        end = new Date();
    }
}

It starts when you call it, and ends when the number of seconds have passed.

查看更多
梦寄多情
6楼-- · 2019-01-01 17:16

Have a look at this excellent brief post on how to do this properly.

Essentially: catch the InterruptedException. Remember that you must add this catch-block. The post explains this a bit further.

查看更多
大哥的爱人
7楼-- · 2019-01-01 17:18

As other users have said you should surround your call with a try{...} catch{...} block. But since Java 1.5 was released, there is TimeUnit class which do the same as Thread.sleep(millis) but is more convenient. You can pick time unit for sleep operation.

try {
    TimeUnit.NANOSECONDS.sleep(100);
    TimeUnit.MICROSECONDS.sleep(100);
    TimeUnit.MILLISECONDS.sleep(100);
    TimeUnit.SECONDS.sleep(100);
    TimeUnit.MINUTES.sleep(100);
    TimeUnit.HOURS.sleep(100);
    TimeUnit.DAYS.sleep(100);
} catch (InterruptedException e) {
    //Handle exception
}

Also it has additional methods: TimeUnit Oracle Documentation

查看更多
登录 后发表回答