如何在 Java 中使用 TimeUnit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47717633/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:46:26  来源:igfitidea点击:

How to use TimeUnit in java

javatimejava.util.concurrent

提问by Ibraheem Alayan

I am trying to use TimeUnit in java but it just send me errors That's how I wrote it

我正在尝试在 Java 中使用 TimeUnit,但它只是向我发送错误这就是我写的

import java.util.concurrent.TimeUnit ;

Public class anything { 
    Public static void main( String[ ] args ) {
        System.out.println(“hi”);
        TimeUnit.SECONDS.sleep(6);
        System.out.println(“hi”);
    }
}

回答by Lalit Verma

Try this as your code have many syntax errorsas well as you are not handling Exceptionduring the call of TimeUnit.SECONDS.sleep(6);, either use throws or surround this with try catch.

试试这个,因为你的代码有很多syntax errors以及你handling Exception在 调用期间没有的TimeUnit.SECONDS.sleep(6);,要么使用 throws,要么用 try catch 包围它。

import java.util.concurrent.TimeUnit ;

public class Anything { 
    public static void main( String[] args ) throws InterruptedException {
        System.out.println("hi");
        TimeUnit.SECONDS.sleep(6);
        System.out.println("hi");
    }
}