Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

Problem with Timer and TimerTask classes

Thread view: 
keweiming@gmail.com - 24 Jun 2005 05:24 GMT
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.
Roland - 24 Jun 2005 10:13 GMT
> Guys,
>
[quoted text clipped - 17 lines]
>     ...
> }

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:

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

>         while(true)
>         {
[quoted text clipped - 4 lines]
>
> // 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.
Signature

Regards,

Roland de Ruiter
` ___      ___
`/__/ w_/ /__/
/  \ /_/ /  \



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.