Running a Java Thread in intervals

后端 未结 3 549
-上瘾入骨i
-上瘾入骨i 2020-12-04 16:34

I have a thread that needs to be executed every 10 seconds. This thread contains several calls (12 - 15) to a database on another server. Additionally, it also accesses arou

3条回答
  •  日久生厌
    2020-12-04 16:55

    Have a look at the Timer and TimerTask classes. They are exactly what you want.

    You can make a TimerTask implementation that takes your thread object in a constructor.

    The run method will then call the threads run method.

    // Perhaps something like this
    Timer t = new Timer();
    t.scheduleAtFixedRate(yourTimerTask, 0, 10 * 1000);
    // Hopefully your task takes less than 12 seconds
    

提交回复
热议问题