Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@spacecow

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

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();を書かないとずっと処理が動くので気をつけましょう!
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?