 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
keweiming@gmail.com Guest
|
Posted: Fri Jun 24, 2005 4:24 am Post subject: Problem with Timer and TimerTask classes |
|
|
Guys,
I have a class that listens to a real-time data source and keep a local
data structure that I want to write out every minute. I plan to use a
TimerTask implementation to do this but aint sure how that can be done.
Bottem of this post has a simplified version of my current code that
listens in to the data source. My question is, if the data is
maintained by a separate thread, how should the TimerTask subclass
and/or run() method be defined to gain access to the external data?
Should I pass it in to the constructor of the subclass of TimerTask?
-km-
// Begining of code.
public class MasterData {
public Hashtable HTdata;
...
public print();
...
}
class mainApp {
public static void main(String[] args)
{
MasterData data=new MasterData();
while(true)
{
// a loop here to update data.HTdata.
}
}
}
// Ending of code.
|
|
| Back to top |
|
 |
Roland Guest
|
Posted: Fri Jun 24, 2005 9:13 am Post subject: Re: Problem with Timer and TimerTask classes |
|
|
On 24-6-2005 6:24, [email]keweiming (AT) gmail (DOT) com[/email] wrote:
| Quote: | Guys,
I have a class that listens to a real-time data source and keep a local
data structure that I want to write out every minute. I plan to use a
TimerTask implementation to do this but aint sure how that can be done.
Bottem of this post has a simplified version of my current code that
listens in to the data source. My question is, if the data is
maintained by a separate thread, how should the TimerTask subclass
and/or run() method be defined to gain access to the external data?
Should I pass it in to the constructor of the subclass of TimerTask?
-km-
// Begining of code.
public class MasterData {
public Hashtable HTdata;
...
public print();
...
}
|
To be able to access the data in the print task, you can either pass an
instance into the PrintDataTask constructor or use "setter" method to
set the data to be printed.
import java.util.TimerTask;
public class PrintDataTask extends TimerTask {
private MasterData data;
public PrintDataTask() {
}
public PrintDataTask(MasterData data) {
this.data = data;
}
public MasterData getData() {
return data;
}
public void run() {
if (data != null) {
data.print();
}
}
public void setData(MasterData data) {
this.data = data;
}
}
Then your mainApp could be like this:
| Quote: | class mainApp {
private final static long ONE_MINUTE_IN_MILLIS = 60L * 1000L;
public static void main(String[] args)
{
MasterData data=new MasterData();
|
Timer taskTimer = new Timer();
PrintDataTask printTask = new PrintDataTask(data);
taskTimer.schedule(printTask, 2000, ONE_MINUTE_IN_MILLIS);
| Quote: | while(true)
{
// a loop here to update data.HTdata.
}
}
}
// Ending of code.
|
The printTask's run method will be called the first time after 2 seconds
(2000 millisecs), and then every minute.
You may need to synchronize the access to the data, i.e. make sure that
the data is not updated while it's being printed by the print task.
Instead of the while-true loop for updating the data, you might consider
to use a scheduled task too, but with much smaller interval than the
print task.
--
Regards,
Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ /_/ /
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|