🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Any known Java Timer event?

#15769
From: Paul Schultz
Date: Wed, 28 Apr 1999 00:00
75 lines
2643 bytes
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: <37273211.2643C89C@ix.netcom.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <01be90b8$364004e0$4e030380@jheuler> <MPG.118fc5a650df85e69899d2@forums.borland.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: Paul Schultz <pasfam@ix.netcom.com>
Subject: Re: Any known Java Timer event?
Date: 1999/04/28
Message-ID: <37273211.2643C89C@ix.netcom.com>
X-Deja-AN: 471930623
Content-Transfer-Encoding: 7bit
References: <01be90b8$364004e0$4e030380@jheuler> <MPG.118fc5a650df85e69899d2@forums.borland.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