0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaのTimerで時間指定の処理を作ろう!!

Last updated at Posted at 2020-09-10

JavaTimer

一定間隔の時間を指定して、処理を実行したいときに使える

JavaTimer
import java.util.Timer;
import java.util.TimerTask;

public class TimeAction {

	public static void main(String[] args) {

        Timer timer = new Timer(); 
        TimerTask tt = new TimerTask() {
            int count = 0;
            public void run() {
                // 定期的に実行したい処理
                count++;
                System.out.println(count + "回目のタスクだよん。");
            }
        };
        //2秒後に、5秒間隔で実行する
        timer.scheduleAtFixedRate(tt,2000,5000); 
    }
//timer.cancel();を書かないとずっと処理が動くので気をつけましょう!
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?