Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

java - 使用Thread.sleep(x)或wait()时出现异常(I get exception when using Thread.sleep(x) or wait())

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

(我试图延迟-或使我的Java程序进入睡眠状态,但是发生错误。)

I'm unable to use Thread.sleep(x) or wait() .

(我无法使用Thread.sleep(x)wait() 。)

The same error message appears:

(出现相同的错误消息:)

unreported exception java.lang.InterruptedException;

(未报告的异常java.lang.InterruptedException;)

must be caught or declared to be thrown.

(必须被抓住或宣布被抛出。)

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

(使用Thread.sleep()wait()方法之前是否需要任何步骤?)

  ask by vincent low translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...