How to handle Timers correctly in Java?

前端 未结 5 410
情书的邮戳
情书的邮戳 2021-01-21 01:13

I want my timer to execute the actionPerformed method only one time once it 5 seconds the time but it is writing in the console \"Hello\" lots of times:

import j         


        
5条回答
  •  不知归路
    2021-01-21 01:37

    class MyTask extends TimerTask {
        public void run() {
          System.out.println("Hello");
    
        }
      }
    

    and then

    timer = new Timer();
    timer.schedule(new MyTask(), 5000);
    

提交回复
热议问题