如何使java延迟几秒钟?
嘿,我有这个密码。我想把程序延迟几秒钟,然后显示“扫描…” 这是我的。这可以编译,但不会延迟任何操作如何使java延迟几秒钟?,java,delay,Java,Delay,嘿,我有这个密码。我想把程序延迟几秒钟,然后显示“扫描…” 这是我的。这可以编译,但不会延迟任何操作 if (i==1){ try { Thread.sleep(1); } catch (InterruptedException ie) { System.out.println("Scanning..."); } } 提前谢谢 显然,我之前有int I=1。sleep(
if (i==1){
try {
Thread.sleep(1);
} catch (InterruptedException ie)
{
System.out.println("Scanning...");
}
}
提前谢谢
显然,我之前有int I=1。sleep()占用的是睡眠的毫秒数,而不是秒数
睡眠一毫秒是不明显的。尝试
Thread.sleep(1000)
睡眠一秒钟。根据java文档,Thread.sleep的定义是:
Thread.sleep(t);
where t => time in millisecons to sleep
如果你想睡1秒钟,你应该:
Thread.sleep(1000);
有几个问题,您没有延迟太多(
。sleep
是毫秒,而不是秒),并且您正在尝试打印catch
语句。您的代码应该更像:
if (i==1) {
try {
System.out.println("Scanning...");
Thread.sleep(1000); // 1 second
} catch (InterruptedException ex) {
// handle error
}
}
在catch块中有
System.out.println(“扫描…”)
。
要将其放入try吗?将System.out语句移动到finally block
这是在一个鼠标事件顺便说一句 如果这是在Swing GUI中,那么去掉对
Thread.sleep(…)
的所有调用,因为这样做会使整个GUI进入睡眠状态,从而使其变得无用。相反,使用Swing计时器在GUI中产生任何延迟,同时让它仍然更新图形
您还需要避免调用System.out.println(…)
,调试时除外,而是在GUI本身中显示用户通知,可能是在状态JLabel中或作为消息对话框显示。Java:
对于计算时间,我们使用以下方法:
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());
}
第二:
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());
输出:
启动延迟10秒,时间为:14:19:08
延迟10秒,时间是:14:19:18
会议记录:
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());
输出:
启动延迟1分钟,时间为:14:21:20
延迟1分钟,时间是:14:22:20
毫秒:
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());
输出:
启动延迟2000毫秒,时间为:14:23:44
延迟2000毫秒,时间是:14:23:46
或:
使用
Thread.sleep(2000)//如果您想暂停,请使用java.util.concurrent.TimeUnit
TimeUnit.SECONDS.sleep(1);
睡一秒钟或10分钟
TimeUnit.MINUTES.sleep(10);
还是线程睡眠
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
另见
TimeUnit.SECONDS.sleep()
将调用Thread.sleep
。唯一的区别是可读性,对于不明显的持续时间,使用时间单位可能更容易理解
但是如果你想解决你的问题
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();
}
int-timeToWait=10//第二
系统输出打印(“扫描”)
试一试{
对于(int i=0;i您需要使用Thread.sleep()
方法
用于将当前线程的执行暂停指定的时间(以毫秒为单位)。毫秒的参数值不能为负,否则会引发IllegalArgumentException
供参考(摘要摘自)
Java线程睡眠要点
- 它总是暂停当前线程的执行
- 线程睡眠不会丢失当前线程拥有的任何监视器或锁
后天的
- 在这种情况下,任何其他线程都可以在睡眠中中断当前线程
抛出case InterruptedException
等待编译器使用此静态类方法的一些时间(比如10秒)
时间单位。秒。睡眠(10);
包括在
java.util.concurrent.TimeUnit libery这是在一个1毫秒的mouseEvent btw中。当你有这样的问题时,请阅读API/手册。哇,我很笨。谢谢:)那是因为你在调用sleep之后才打印。是的,我知道了。对不起,我很笨。谢谢大家。谢谢大家,我很笨…:p
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();
}
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (getActivity() != null)
getActivity().runOnUiThread(() -> tvCovidAlert.startAnimation(animBounce));
}
}, DELAY_TIME_MILI_SECONDS);