I would like to pass a variable to the run method in java.util.TimerTask.* - how do I do this?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Iain_Kerr
Posted On:   Friday, April 23, 2004 07:18 AM

I would like to pass a variable to the run method in java.util.TimerTask.* - how do I do this?

Re: I would like to pass a variable to the run method in java.util.TimerTask.* - how do I do this?

Posted By:   Sean_Owen  
Posted On:   Friday, April 23, 2004 12:44 PM

What Bill said -- here's an example:


public class MyTimerTask extends TimerTask {

private String aValue;

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

public void run() {
System.out.println(aValue);
}

}

...

// pass "foo" to your TimerTask
MyTimerTask tt = new MyTimerTask("foo");
// schedule the task...


This is a pattern than comes up a lot, I think.

Re: I would like to pass a variable to the run method in java.util.TimerTask.* - how do I do this?

Posted By:   Bill_Scott  
Posted On:   Friday, April 23, 2004 12:26 PM

You don't. Timers take a subclass of "TimerTask". Create a data member on *your* subclass of TimerTask for your argument (passed-in during construction or via a mutator). Then deferrerence during "run".


-Bill

About | Sitemap | Contact