I get exception when using Thread.sleep(x) or wait()
我试图延迟或睡觉-我的Java程序,但发生错误。
我不能使用
unreported exception java.lang.InterruptedException; must be caught or declared to be thrown.
在使用
你前面有很多书要读。从编译器错误到异常处理、线程和线程中断。但这将满足您的需求:
1 2 3 4 5 | try { Thread.sleep(1000); //1000 milliseconds is one second. } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } |
正如其他用户所说,您应该用一个
1 2 3 4 5 6 7 8 9 10 11 | 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 } |
此外,它还有其他方法:时间单位Oracle文档
请看一看这篇关于如何正确地完成这项工作的精彩的简短文章。
本质上:抓住
使用以下编码构造处理异常
1 2 3 4 5 |
把你的
1 2 3 4 5 6 | try { //thread to sleep for the specified number of milliseconds Thread.sleep(100); } catch ( java.lang.InterruptedException ie) { System.out.println(ie); } |
当使用Android(我使用Java的唯一时间)时,我建议使用一个处理程序,而不是让线程睡觉。
1 2 3 4 5 6 7 8 | 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); |
参考:http://developer.android.com/reference/android/os/handler.html
我给Java程序增加延迟的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public void pause1(long sleeptime) { try { Thread.sleep(sleeptime); } catch (InterruptedException ex) { //ToCatchOrNot } } public void pause2(long sleeptime) { Object obj = new Object(); if (sleeptime > 0) { synchronized (obj) { try { obj.wait(sleeptime); } catch (InterruptedException ex) { //ToCatchOrNot } } } } public void pause3(long sleeptime) { expectedtime = System.currentTimeMillis() + sleeptime; while (System.currentTimeMillis() < expectedtime) { //Empty Loop } } |
这是为了顺序延迟,但是对于循环延迟而言,是指Java延迟/等待。
1 2 3 4 5 6 7 8 9 | public static void main(String[] args) throws InterruptedException { //type code short z=1000; Thread.sleep(z);/*will provide 1 second delay. alter data type of z or value of z for longer delays required */ //type code } |
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class TypeCasting { public static void main(String[] args) throws InterruptedException { short f = 1; int a = 123687889; short b = 2; long c = 4567; long d=45; short z=1000; System.out.println("Value of a,b and c are " + a +" " + b +" " + c +"respectively"); c = a; b = (short) c; System.out.println("Typecasting..........."); Thread.sleep(z); System.out.println("Value of B after Typecasting" + b); System.out.println("Value of A is" + a); } } |
试试这个:
但请不要使用
使用"线程等待"概念的多线程/多核Java应用程序的最佳实践。wait释放线程持有的所有锁和监视器,这允许其他线程获取这些监视器,并在您的线程平和睡眠时继续。
下面的代码演示了该技术:
1 2 3 4 5 6 7 8 9 10 11 | import java.util.concurrent.TimeUnit; public class DelaySample { public static void main(String[] args) { DelayUtil d = new DelayUtil(); System.out.println("started:"+ new Date()); d.delay(500); System.out.println("half second after:"+ new Date()); d.delay(1, TimeUnit.MINUTES); System.out.println("1 minute after:"+ new Date()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class DelayUtil { /** * Delays the current thread execution. * The thread loses ownership of any monitors. * Quits immediately if the thread is interrupted * * @param durationInMillis the time duration in milliseconds */ public void delay(final long durationInMillis) { delay(durationInMillis, TimeUnit.MILLISECONDS); } /** * @param duration the time duration in the given {@code sourceUnit} * @param unit */ public void delay(final long duration, final TimeUnit unit) { long currentTime = System.currentTimeMillis(); long deadline = currentTime+unit.toMillis(duration); ReentrantLock lock = new ReentrantLock(); Condition waitCondition = lock.newCondition(); while ((deadline-currentTime)>0) { try { lock.lockInterruptibly(); waitCondition.await(deadline-currentTime, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return; } finally { lock.unlock(); } currentTime = System.currentTimeMillis(); } } } |
使用
1 | TimeUnit.SECONDS.sleep(1); |
睡眠一秒钟或
1 | TimeUnit.MINUTES.sleep(1); |
睡一分钟。
因为这是一个循环,所以这就提出了一个固有的问题——漂移。每次你运行代码然后睡觉的时候,你都会从运行中偏离一点,比如说,每秒钟。如果这是一个问题,那么不要使用
而且,在控制方面,
对于每秒钟或每延迟一秒运行一个任务,我强烈建议使用[
运行方法EDCOX1,10秒每秒钟(Java 8):
1 2 3 4 5 6 7 8 |
等待的一个简单方法是使用
1 2 3 4 5 6 7 8 9 10 |
这样,您就不必再为线程和异常操心了。希望这有帮助!
或者,如果不想处理线程,请尝试以下方法:
1 2 3 4 5 6 7 |
它从您调用它时开始,到经过秒数时结束。