Easily implementing a Timer

D

DaveN

Hi All,

I've just started with Java in the last couple of days and am now having
some trouble finding a clear description of implementing a timer, this
however may be down to my lack of experience with OOP, so I apologise if
that's the case! :)

I understand the need for the java.util.timer/Task classes to get the
correct things available to me.

I need to have a timer running in a background thread that calls a function
every say 1s. That function will do some things like updating a screen
message and some other application calculations. It also needs to stop the
timer when the necessary conditions are met. The timer will have been
started when the user requests.

My pseudo java code for this is something like:


void MainFunction()
{
if(UserMakesStartSelection == 1)
{
Timer timer = new CreateNewTimer( TimerFunctionToCall, StartNow,
Every1000ms)
}
}


void TimerFunctionToCall()
{
/* Gets called every 1000ms */
/* Do some things */
GetTimeElapsed();
UpdateScreen();
.
.
.
etc

/* Check conditions */
if(TimeToStop == 1)
{
StopTimer(timer);
}
}

All the examples I've seen seem to have these functions all wrapped together
in a class which makes it difficult to understand and follow. Like I say
it's probably my lack of OOP but if anyone can help to clear the muddy
waters I would appreciate it.
 
S

Stefan Ram

DaveN said:
void MainFunction()

In Java, »function« usually does not have the implied meaning.

Class declarations contain /method/ declarations.
if(UserMakesStartSelection == 1)

The user should not be able to create multiple timers in
parallel this way. Also, user events often are transported as
invocations, so often one will not use »if«, but an event
handling method.
void TimerFunctionToCall()

To be able to designate a method as an argument value (as
above), it needs to be wrapped in some object. There are no
method references in Java - only object references. And method
names usually start with a lower-case letter.
All the examples I've seen seem to have these functions all
wrapped together in a class which makes it difficult to
understand and follow. Like I say it's probably my lack of OOP
but if anyone can help to clear the muddy waters I would
appreciate it.

You do not need to learn OOP (as in Smalltalk), it is
sufficient to learn Java. Possibly, start with the
fundamentals and then read approach more complicated topics
later.

1st step: Learn about invocations of operations (methods)
including argument passing, learn to use standard types
(primitive types, classes and interfaces including standard
methods), learn to read JavaDocs, learn the terms. take some
time for exercises. Take care to learn the difference between
static and non-static operations (AKA methods).

2nd step: Learn how to write and use declarations for
variables, methods, classes, interfaces, and JavaDoc. Do more
exercises.

3rd step: Learn fundamentals of Swing (like the EDT and the
delegation event model) and something about threads. Learn
some common patterns, like »Observer«.
 
D

DaveN

DaveN said:
Thanks Roedy, that looks like a much easier to follow example.


Quick follow up, implemented from your example and I now have exactly as I
required.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top