Time interval in Java

后端 未结 2 460
长情又很酷
长情又很酷 2020-12-21 21:58

how to call a method after a time interval? e.g if want to print a statement on screen after 2 second, what is its procedure?

System.out.println(\"Printing s         


        
2条回答
  •  我在风中等你
    2020-12-21 22:29

    Create a Class:

    class SayHello extends TimerTask {
        public void run() {
           System.out.println("Printing statement after every 2 seconds"); 
        }
    }
    

    Call the same from your main method:

    public class sample {
        public static void main(String[] args) {
            Timer timer = new Timer();
            timer.schedule(new SayHello(), 2000, 2000);
    
        }
    }
    

提交回复
热议问题