Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to count n seconds and change value

Status
Not open for further replies.

justride

Programmer
Jan 9, 2004
251
US
Hi all,

I have an instanc of this class A that does some things... Other instances of other classes access this instance A all the time, specificly a hashtable of data. I need to reset this hashtable every 8 seconds. Without setting a delay, how can I do this. Do I need to create another instance of soem time counter or something, count 8 seconds then return to instance A?

Could somebody offer advice?

Thanks
Chris
 
Oh yea, Instance A needs to be accessible at all times.

Thanks
 
And I need to be able to halt the 8 second count and start over at anytime.
 
Have u tried javax.swing.Timer?

Code:
  int delay = 8000; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //...Perform a task...
      }
  };
  new Timer(delay, taskPerformer).start();

Chinese Java Faq Forum
 
I am implementing Timer and TimerTask. Thanks for the advice, I will post if I have any issues.

 
Stefan, Hashtable is already internally synchronized.

Tim
 
Here is the deal,

Everytime my propertychanged method is called, I want to restart the 8 second timer so I do this:
Code:
 _transTimer = new Timer();
 _transTimer.schedule(new TransactionTimerTask(this),8000);
Before I was cancelling the existign task before creating a new one but I was receving an exception that I cannot start a cancelled task.

Anyways, this way works, and my hashtable clears every 8 seconds, but sometimes the timing is off, off my a bit, so I am thinking that this may not be the perfect way to do this.

For example, if I call this property change method 4 seconds into a scheduled task, then my new task doesnt appear to run for a new 8 seconds, it seems to only run for the remaining 4 of the last task.

Does anybody know how I can stop the existing task and start a new one for 8 seconds?

This could be the right way and I am just experiencing delays because there are a lot of other things going on but I am not quite sure.

Thanks
 
Oh yea, the "this" instance contains the resetHashtable() method.
 
Hello,

I just did some serious testing. It seems that new tasks do not get scheduled properly. I need to somehow, disregard the previous task if a new one comes in within 8 seconds of the last task. My logic is only working if new tasks come in after 8 seconds.

Thanks
 
Well - I remember the usage of Vector and Hashtable was discouraged because they are internally synchronized, but without reaching the goal.

But I don't find any hint in the documentation - perhaps they fixed these bugs.

seeking a job as java-programmer in Berlin:
 
I appear to have this working now, after cancelling the task I set it to null before proceeding to the recreation.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top