简体   繁体   English

如何将参数传递给Timertask Run方法

[英]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. 我已经尝试过Timers,但我有一个问题。

How could It be possible to pass arguments to Java TimerTask run method ? 怎么可能将参数传递给Java TimerTask运行方法?

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

You can write you own class which extends from TimerTask class and you can override run method. 您可以编写自己的类,它从TimerTask类扩展,您可以覆盖run方法。

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

您需要扩展TimerTask并创建构造函数和/或setter字段。然后在调度TimerTask执行之前设置所需的值。

The only way to do this is to create your own class that extends TimerTask and pass arguments to its constructor or call its setters. 执行此操作的唯一方法是创建自己的类,该类扩展TimerTask并将参数传递给其构造函数或调用其setter。 So, the task will "know" its arguments from the moment of its creation. 因此,任务将从其创建的那一刻起“知道”它的论点。

Moreover if it provides setters you can modify the task configuration even later, after the task has been already scheduled. 此外,如果它提供了setter,您甚至可以在已经安排任务之后修改任务配置。

It's not possible to change the signature of the run() method. 无法更改run()方法的签名。

However, you may create a subclass of TimerTask and give it some kind of initialize-method. 但是,您可以创建TimerTask的子类并为其提供某种初始化方法。 Then you can call the new method with the arguments you want, save them as fields in your subclass and then reference those initialized fields in the run() -method: 然后,您可以使用所需的参数调用new方法,将它们保存为子类中的字段,然后在run() -method中引用这些初始化字段:

abstract class MyTimerTask extends TimerTask
{
  protected String myArg = null;
  public void init(String arg)
  {
    myArg = arg;
  }
}

...

MyTimerTask timert = new MyTimerTask() 
{
  @Override
  public void run() 
  {
       //do something
       System.out.println(myArg);
  }
} 

...

timert.init("Hello World!");
new Thread(timert).start();

Make sure to set the fields' visibilities to protected , because private fields are not visible to (anonymous) subclasses of MyTimerTask . 确保将字段的可见性设置为protected ,因为private字段对MyTimerTask (匿名)子类不可见。 And don't forget to check if your fields have been initialized in the run() method. 并且不要忘记检查您的字段是否已在run()方法中初始化。

class thr extends TimerTask{    
@Override
public void run(){
    System.out.println("task executed task executed .........." );
}               


class service { 
private Timer t;    
public void start(int delay){
    Timer timer=new Timer();
    thr task =new thr();                        
    timer.schedule(task, delay);        
    this.t=timer;
}   
public void stop(){
    System.out.println("Cancelling the task scheduller ");
    this.t.cancel();
}
}

public class task1 {
  public static void main(String[] args) {

    service sv=new service();
    sv.start(1000);
    System.out.println("This will check the asynchronous behaviour ");
    System.out.println("This will check the asynchronous behaviour ");
    System.out.println("This will check the asynchronous behaviour ");
    System.out.println("This will check the asynchronous behaviour ");
    System.out.println("This will check the asynchronous behaviour ");
    sv.stop();

  } 
}

the code above works fine to schedule and reschedule the task you can set and reset the task with a timer function 上面的代码可以很好地安排和重新安排您可以设置的任务,并使用计时器功能重置任务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM