Article View: borland.public.jbuilder.java.language
Article #15712Re: Any known Java Timer event?
From: Tad Frysinger
Date: Fri, 30 Apr 1999 00:00
Date: Fri, 30 Apr 1999 00:00
58 lines
2050 bytes
2050 bytes
In article <37273211.2643C89C@ix.netcom.com>, pasfam@ix.netcom.com
says...
> Tad,
>
> Try this inner class implementation of a timer.
>
> First, the TimerThread class, implemented as an inner class so it can
> call a method in its outer class:
>
> /**
> * Implement an inner timer class as a Thread.
> */
> protected class TimerThread extends Thread
> {
> long milliseconds;
>
> public TimerThread(long _milliseconds)
> {
> milliseconds = _milliseconds;
> }
>
> public void run()
> {
> while (true)
> {
> try
> {
> sleep(milliseconds);
> outerClassMethod(); // Whatever you want, could fire off an event
> instead
> }
> catch (InterruptedException ex)
> {
> }
> }
> }
> }
>
> Add an instance variable reference for the timer:
>
> protected TimerThread timer;
>
> Then just create an instance with some number of milliseconds specified:
>
> protected void someMethod()
> {
> TimerThread timer = new TimerThread(1000 * 60 * 15); // 15 minutes
> timer.start();
> }
>
> That's all it is, no "limited Windows resources" or other restrictions, you can
> have as many as you want. Fire off an event instead of calling a method
This is good for some purposes and I'm glad you posted for folks to see,
but Tom beat me to my response: it doesn't keep timing while you execute
the method.
Tad
Technical Details
Message-ID:
<MPG.1193bdbbb5433d1a9899d5@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<01be90b8$364004e0$4e030380@jheuler> <MPG.118fc5a650df85e69899d2@forums.borland.com> <37273211.2643C89C@ix.netcom.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,e0bb2eb35266f646 X-Google-Attributes: gidf74b3,public From: tfrysinger@earthlink.net (Tad Frysinger) Subject: Re: Any known Java Timer event? Date: 1999/04/30 Message-ID: <MPG.1193bdbbb5433d1a9899d5@forums.borland.com> X-Deja-AN: 472871101 References: <01be90b8$364004e0$4e030380@jheuler> <MPG.118fc5a650df85e69899d2@forums.borland.com> <37273211.2643C89C@ix.netcom.com> Organization: Freezone Enterprises Newsgroups: borland.public.jbuilder.java.language