【问题标题】:How to Pass Arguments to Timertask Run Method如何将参数传递给 Timertask 运行方法
【发布时间】:2011-09-22 08:57:14
【问题描述】:

我有一个方法,我希望它被安排在以后执行。调度时间和方法的参数取决于用户输入。

我已经尝试过 Timers,但我有一个问题。

如何将参数传递给 Java TimerTask 运行方法?

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

【问题讨论】:

  • 将参数设置为字段,然后您可以在运行方法中获取它

标签: java scheduling timertask


【解决方案1】:

您可以编写自己的类,该类从 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 
     }
}

【讨论】:

  • +1 为详细示例,但请大写单词“String”。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    无法更改run() 方法的签名。

    但是,您可以创建 TimerTask 的子类并为其提供某种初始化方法。然后您可以使用所需的参数调用新方法,将它们保存为子类中的字段,然后在run()-方法中引用这些初始化的字段:

    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();
    

    确保将字段的可见性设置为protected,因为private 字段对MyTimerTask 的(匿名)子类不可见。并且不要忘记检查您的字段是否已在 run() 方法中初始化。

    【讨论】:

      【解决方案4】:

      做到这一点的唯一方法是创建您自己的类来扩展 TimerTask 并将参数传递给它的构造函数或调用它的 setter。因此,任务从创建的那一刻起就“知道”它的论点。

      此外,如果它提供设置器,您甚至可以在任务已经安排好之后修改任务配置。

      【讨论】:

        【解决方案5】:
        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();
        
          } 
        }
        

        上面的代码可以很好地安排和重新安排您可以使用计时器功能设置和重置任务的任务

        【讨论】:

          猜你喜欢
          • 2018-05-22
          • 2014-11-14
          • 2011-07-18
          • 2021-12-13
          • 2012-02-23
          • 2011-07-25
          • 2023-04-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多