我正在处理一个Java应用程序,我想暂停执行几秒钟(由用户选择),然后继续执行,后面的..and是一个简单的代码:
Public Class myApp
{
static public main()
{
int seconds=10;
// do couple of things
try {
Thread.sleep(10*1000);
} catch (InterruptedException ex) {
Logger.getLogger(myApp.class.getName()).log(Level.SEVERE, null, ex);
}
}我现在想给用户一个机会,点击一个按钮来恢复执行,甚至在时间结束之前。这可以使用"Thread.sleep()“吗?还是有另外一种方法来暂停应用程序并恢复它呢?
发布于 2013-09-24 03:36:16
使用CountDownLatch
Public Class myApp
{
static CountDownLatch countDownLatch = new CountDownLatch(1);
static public main()
{
try {
countDownLatch.await(10000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ex) {
// Logging
}
}
},您可以在单击“简历”按钮调用countDownLatch.countDown()方法。
发布于 2013-09-24 03:25:01
您可以使用中断取消睡眠,下面是更多信息:http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html
https://stackoverflow.com/questions/18972387
复制相似问题