Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
     }
}   
like image 997
sjtaheri Avatar asked Sep 22 '11 08:09

sjtaheri


2 Answers

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 
     }
}
like image 150
onurbaysan Avatar answered Sep 28 '22 03:09

onurbaysan


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.

like image 38
sethu Avatar answered Sep 28 '22 05:09

sethu