How do I make a delay in Java?

前端 未结 8 1394
Happy的楠姐
Happy的楠姐 2020-11-22 08:24

I am trying to do something in Java and I need something to wait / delay for an amount of seconds in a while loop.

while (true) {
    if (i == 3) {
        i         


        
8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 09:12

    Use this:

    public static void wait(int ms)
    {
        try
        {
            Thread.sleep(ms);
        }
        catch(InterruptedException ex)
        {
            Thread.currentThread().interrupt();
        }
    }
    

    and, then you can call this method anywhere like:

    wait(1000);
    

提交回复
热议问题