如何让java延迟几秒钟?

时间:2014-04-25 02:10:15

标签: java delay

嘿,我有这个代码。我想延迟我的程序几秒钟并显示“扫描......”

这就是我所拥有的。这会编译但不会延迟任何事情

 if (i==1){

        try {
            Thread.sleep(1);
        } catch (InterruptedException ie)
        {
            System.out.println("Scanning...");
        }

    }
提前谢谢 在显然

之前我有int i = 1

12 个答案:

答案 0 :(得分:7)

如果您想暂停,请使用java.util.concurrent.TimeUnit

TimeUnit.SECONDS.sleep(1);

睡一秒或10分钟

TimeUnit.MINUTES.sleep(10);

或线程睡眠

try        
{
    Thread.sleep(1000);
} 
catch(InterruptedException ex) 
{
    Thread.currentThread().interrupt();
}

另见official documentation

TimeUnit.SECONDS.sleep()会致电Thread.sleep。唯一的区别是可读性,使用TimeUnit可能更容易理解非显而易见的持续时间。

但如果你想解决问题

        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();
        }

答案 1 :(得分:2)

Thread.sleep()占用睡眠的毫秒数,而不是秒数。

睡眠一毫秒并不明显。尝试Thread.sleep(1000)睡一秒钟。

答案 2 :(得分:2)

您在catch区块中有System.out.println("Scanning...")。 你想把它放在试试中吗?

答案 3 :(得分:2)

  

这是在mouseEvent btw

如果这是在Swing GUI中,那么去除对Thread.sleep(...)的所有调用,因为这样做会使整个GUI进入睡眠状态,使其无效。而是使用Swing Timer在GUI中产生任何延迟,同时让它仍然更新其图形。

除了调试时,您还希望避免System.out.println(...)次调用,而是在GUI本身中显示用户通知,可能是状态JLabel或消息对话框。

答案 4 :(得分:2)

<强>爪哇:

为计算时间,我们使用此方法:

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(1000); //milliseconds

答案 5 :(得分:1)

根据java文档中Thread.sleep的定义是:

Thread.sleep(t);
where t => time in millisecons to sleep

如果你想睡一秒钟,你应该:

Thread.sleep(1000);

答案 6 :(得分:1)

有几个问题,你没有太多延迟(.sleep是毫秒,而不是秒),而你正试图在你的catch声明中打印。您的代码看起来应该更像:

if (i==1) {
    try {
        System.out.println("Scanning...");
        Thread.sleep(1000); // 1 second
    } catch (InterruptedException ex) {
        // handle error
    }
}

答案 7 :(得分:1)

 new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    if (getActivity() != null)
                        getActivity().runOnUiThread(() -> tvCovidAlert.startAnimation(animBounce));
                }
            }, DELAY_TIME_MILI_SECONDS);

答案 8 :(得分:0)

将System.out语句移动到finally块。

答案 9 :(得分:0)

使用Thread.sleep(2000); // 2000 2秒

答案 10 :(得分:0)

您需要使用Thread.sleep()方法。

这用于在毫秒的指定时间内暂停当前线程的执行。以毫秒为单位的参数值不能为负,否则将引发 IllegalArgumentException

仅供参考(摘录自here

Java线程睡眠要点

  • 它总是暂停当前线程的执行。
  • 线程睡眠不会丢失任何监视器或锁定当前线程拥有的 获得。
  • 任何其他线程都可以在睡眠中中断当前线程,因为 引发InterruptedException情况。

答案 11 :(得分:0)

等待一段时间(例如10秒钟),编译器将使用此静态类方法
TimeUnit.SECONDS.sleep(10);
它包含在 java.util.concurrent.TimeUnit libery