开发者

question related with java

开发者 https://www.devze.com 2023-02-23 00:02 出处:网络
Can you please suggest how to use until command in Java, actually I have to 开发者_如何学Goperform thisSystem.exit(0); after 3 second of current system time. So I am thinking to do by long time=System

Can you please suggest how to use until command in Java, actually I have to 开发者_如何学Goperform this System.exit(0); after 3 second of current system time. So I am thinking to do by long time=System.currentTimeMillis();

until(System.currentTimeMillis()<(time+3000))
{
    System.exit(0);
}

But it reports an error


Actually java does not have an until command but you should use a while-loop or a do-while-loop instead.

Note: Thread.sleep(3000); would be a better way to sleep for three seconds.


I might have misunderstood your requirement but if you just want to wait for 3 seconds then call System.exit(0), you can just use:

Thread.sleep(3000);
System.exit(0);

I apologise if I have misunderstood your question.


If you're trying to wait for a specific period of time constantly polling on the elapsed system time is not the way to go. Alternatively you can use the thread scheduler to pause execution of the current thread and request that the JVM notify you when the time is up. This allows other threads in a multithreaded environment to get things done while you wait.

try {
    Thread.sleep(3000);
} catch (InterruptedException ex) {
    // Insert appropriate exception handling here...
}

Thread.sleep makes the currently executing thread move to the ready state for 3000ms. The JVM is then responsible for moving the thread back to the running state when at least 3 seconds is up.

0

精彩评论

暂无评论...
验证码 换一张
取 消