如何让java延迟几秒钟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23283041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make java delay for a few seconds?
提问by user2918193
Hey all I have this code. I want to delay my program for a few seconds and display "scanning..."
嘿,我有这个代码。我想延迟我的程序几秒钟并显示“正在扫描...”
Here's what I have. This compiles but doesn't delay anything
这就是我所拥有的。这会编译但不会延迟任何事情
if (i==1){
try {
Thread.sleep(1);
} catch (InterruptedException ie)
{
System.out.println("Scanning...");
}
}
thanks in advance I have int i = 1 before obviously
在此先感谢我之前显然有 int i = 1
回答by Oleksi
Thread.sleep()
takes in the number of milliseconds to sleep, not seconds.
Thread.sleep()
需要睡眠的毫秒数,而不是秒数。
Sleeping for one millisecond is not noticeable. Try Thread.sleep(1000)
to sleep for one second.
休眠一毫秒并不明显。试着Thread.sleep(1000)
睡一秒钟。
回答by Kakarot
As per java documentation definition of Thread.sleep is :
根据 Thread.sleep 的 java 文档定义是:
Thread.sleep(t);
where t => time in millisecons to sleep
If you want to sleep for 1 second you should have :
如果你想睡一秒钟,你应该:
Thread.sleep(1000);
回答by Rogue
A couple problems, you aren't delaying by much (.sleep
is milliseconds, not seconds), and you're attempting to print in your catch
statement. Your code should look more like:
有几个问题,您并没有延迟太多(.sleep
是毫秒,而不是秒),并且您正试图在您的catch
语句中打印。您的代码应该看起来更像:
if (i==1) {
try {
System.out.println("Scanning...");
Thread.sleep(1000); // 1 second
} catch (InterruptedException ex) {
// handle error
}
}
回答by DreamUth
You have System.out.println("Scanning...")
in a catch block.
Do you want to put that in try?
你有System.out.println("Scanning...")
一个catch块。你想试试吗?
回答by sps
move the System.out statement to finally block.
将 System.out 语句移至 finally 块。
回答by Hovercraft Full Of Eels
This is in a mouseEvent btw
这是在 mouseEvent 中
If this is in a Swing GUI, then get rid of all calls to Thread.sleep(...)
as doing so can put the entire GUI to sleep rendering it useless. Instead use a Swing Timer to produce any delays in the GUI while letting it still update its graphics.
如果这是在 Swing GUI 中,则摆脱所有对 的调用,Thread.sleep(...)
因为这样做会使整个 GUI 进入休眠状态,使其变得无用。而是使用 Swing Timer 在 GUI 中产生任何延迟,同时让它仍然更新其图形。
You'll also want to avoid System.out.println(...)
calls, except when debugging, and instead display user notifications in the GUI itself, perhaps in a status JLabel or as a message dialog.
您还需要避免System.out.println(...)
调用(调试时除外),而是在 GUI 本身中显示用户通知,可能在状态 JLabel 中或作为消息对话框显示。
回答by Gavriel Cohen
Java:
爪哇:
For calculating times we use this method:
为了计算时间,我们使用这种方法:
import java.text.SimpleDateFormat;
import java.util.Calendar;
public static String getCurrentTime() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(cal.getTime());
}
Second:
第二:
import java.util.concurrent.TimeUnit;
System.out.println("Start delay of 10 seconds, Time is: " + getCurrentTime());
TimeUnit.SECONDS.sleep(10);
System.out.println("And delay of 10 seconds, Time is: " + getCurrentTime());
Output:
输出:
Start delay of 10 seconds, Time is: 14:19:08
And delay of 10 seconds, Time is: 14:19:18
启动延迟10秒,时间为:14:19:08
并延迟10秒,时间为:14:19:18
Minutes:
分钟:
import java.util.concurrent.TimeUnit;
System.out.println("Start delay of 1 Minute, Time is: " + getCurrentTime());
TimeUnit.MINUTES.sleep(1);
System.out.println("And delay of 1 Minute, Time is: " + getCurrentTime());
Output:
输出:
Start delay of 1 Minute, Time is: 14:21:20
And delay of 1 Minute, Time is: 14:22:20
启动延迟1分钟,时间为:14:21:20
并延迟1分钟,时间为:14:22:20
Milliseconds:
毫秒:
import java.util.concurrent.TimeUnit;
System.out.println("Start delay of 2000 milliseconds, Time is: " +getCurrentTime());
TimeUnit.MILLISECONDS.sleep(2000);
System.out.println("And delay of 2000 milliseconds, Time is: " + getCurrentTime());
Output:
输出:
Start delay of 2000 milliseconds, Time is: 14:23:44
And delay of 2000 milliseconds, Time is: 14:23:46
启动延迟2000毫秒,时间为:14:23:44
并且延迟2000毫秒,时间为:14:23:46
Or:
或者:
Thread.sleep(1000); //milliseconds
回答by Crime_Master_GoGo
Use Thread.sleep(2000);
//2000 for 2 seconds
使用Thread.sleep(2000);
//2000 2 秒
回答by venergiac
If you want to pause then use java.util.concurrent.TimeUnit
:
如果要暂停,请使用java.util.concurrent.TimeUnit
:
TimeUnit.SECONDS.sleep(1);
To sleep for one second or for 10 minutes
睡一秒钟或十分钟
TimeUnit.MINUTES.sleep(10);
Or Thread Sleep
或线程睡眠
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
see also the official documentation
另见官方文档
TimeUnit.SECONDS.sleep()
will call Thread.sleep
. The only difference is readability and using TimeUnit is probably easier to understand for non obvious durations.
TimeUnit.SECONDS.sleep()
会打电话Thread.sleep
。唯一的区别是可读性,对于不明显的持续时间,使用 TimeUnit 可能更容易理解。
but if you want to solve your issue
但如果你想解决你的问题
int timeToWait = 10; //second
System.out.print("Scanning")
try {
for (int i=0; i<timeToWait ; i++) {
Thread.sleep(1000);
System.out.print(".")
}
} catch (InterruptedException ie)
{
Thread.currentThread().interrupt();
}
回答by Dulith De Costa
You need to use the Thread.sleep()
method.
您需要使用该Thread.sleep()
方法。
This is used to pause the execution of current thread for specified time in milliseconds. The argument value for milliseconds can't be negative, else it throws IllegalArgumentException.
这用于将当前线程的执行暂停指定的时间(以毫秒为单位)。毫秒的参数值不能为负数,否则会抛出IllegalArgumentException。
FYI (Summary taken from here)
仅供参考(摘自此处的摘要)
Java Thread Sleep important points
Java Thread Sleep 要点
- It always pause the current thread execution.
- Thread sleep doesn't lose any monitors or locks current thread has acquired.
- Any other thread can interrupt the current thread in sleep, in that case InterruptedException is thrown.
- 它总是暂停当前线程的执行。
- 线程睡眠不会丢失任何监视器或锁定当前线程已获得。
- 任何其他线程都可以在睡眠状态下中断当前线程,在这种情况下会抛出 InterruptedException。