Search code examples
javaschedulingtimertask

How to Pass Arguments to Timertask Run Method


I have a method and I want it to be scheduled for execution in later times. The scheduling time and method's arguments depend on user inputs.

I already have tried Timers, but I have a question.

How could It be possible to pass arguments to Java TimerTask run method ?

TimerTask timert = new TimerTask() 
{
     @Override
     public void run() 
     {
           //do something
     }
}   

Solution

  • You will need to extend the TimerTask and create a constructor and/or setter fields.. Then set the values you want before scheduling the TimerTask for execution.