How to Pass Arguments to Timertask Run Method

sjtaheri picture sjtaheri · Sep 22, 2011 · Viewed 18.9k times · Source

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
     }
}   

Answer

onurbaysan picture onurbaysan · Sep 22, 2011

You can write you own class which extends from TimerTask class and you can override run method.

class MyTimerTask extends TimerTask  {
     String param;

     public MyTimerTask(String param) {
         this.param = param;
     }

     @Override
     public void run() {
         // You can do anything you want with param 
     }
}