🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #15771

Re: Any known Java Timer event?

#15771
From: Tom Heagy
Date: Wed, 28 Apr 1999 00:00
83 lines
3289 bytes
This works well as long as the "outerClassMethod" doesn't take long to execute.  If
it IS a long running method, the timing will no longer be accurate.  This can be
aleviated by having the timer create a new thread every time it executes
"outerClassMethod" so that the timer will immediately [well almost] go back to
sleep.  This gets a bit messy with inner classes so I would recommend creating the
timer class as an external class.  Just my 2 cents...

Paul Schultz wrote:

> 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
> and TimerThread doesn't have to be an inner class. Make a constructor
> where you can pass in an object as an interface reference and you can
> call a method defined by that interface. Call timer.stop() when you are done
>
> Hope this helps,
> Paul Schultz
> pschultz@csc.cps.k12.il.us
>
> Tad Frysinger wrote:
>
> > In article <01be90b8$364004e0$4e030380@jheuler>, joeh@choruscall.com
> > says...
> > > Does anyone know if a Timer type event exists within Java?  I have
> > > constructed an applet using JBuilder2 and I would like to a check if a
> > > certain condition every n seconds.  Is there a way to do this within Java?
> > >
> > > Thank you in advance
> > > Joe Heuler
> > >
> > Only in java.awt.Swing, which makes for a pretty hefty download if all
> > you want is a timer.  You might search JARS et al for a small Timer
> > component.
> >
> > Tad Frysinger (TeamB)
Technical Details
Message-ID: <37273E4B.5A568415@sciatl.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <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: Tom Heagy <tom.heagy@sciatl.com>
Subject: Re: Any known Java Timer event?
Date: 1999/04/28
Message-ID: <37273E4B.5A568415@sciatl.com>
X-Deja-AN: 471943692
Content-Transfer-Encoding: 7bit
References: <01be90b8$364004e0$4e030380@jheuler> <MPG.118fc5a650df85e69899d2@forums.borland.com> <37273211.2643C89C@ix.netcom.com>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language