java 如何将参数传递给 Timertask Run 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7512157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Pass Arguments to Timertask Run Method
提问by sjtaheri
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 ?
怎么可能将参数传递给 Java TimerTask 运行方法?
TimerTask timert = new TimerTask()
{
@Override
public void run()
{
//do something
}
}
回答by onurbaysan
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
}
}
回答by sethu
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.
您将需要扩展TimerTask
并创建一个构造函数和/或 setter 字段。然后在安排TimerTask
执行之前设置您想要的值。
回答by hiergiltdiestfu
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. 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:
但是,您可以创建 TimerTask 的子类并为其提供某种初始化方法。然后你可以用你想要的参数调用新方法,将它们保存为子类中的字段,然后在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
. And don't forget to check if your fields have been initialized in the run()
method.
确保将字段的可见性设置为protected
,因为private
字段对 的(匿名)子类不可见MyTimerTask
。并且不要忘记检查您的字段是否已在run()
方法中初始化。
回答by AlexR
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. So, the task will "know" its arguments from the moment of its creation.
唯一的方法是创建您自己的类来扩展 TimerTask 并将参数传递给它的构造函数或调用它的 setter。因此,任务从创建的那一刻起就会“知道”它的参数。
Moreover if it provides setters you can modify the task configuration even later, after the task has been already scheduled.
此外,如果它提供了 setter,您甚至可以在任务已经安排好之后修改任务配置。
回答by Gyanender Singhle
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
上面的代码可以很好地安排和重新安排您可以使用计时器功能设置和重置任务的任务