Solved

Java Delay/Timer Question

Posted on 2001-09-06
4
199 Views
Last Modified: 2013-11-24
Hi,

I'm trying to do something with a java servlet
involving a time dealy ...

Basically what happens is i get an alert over the
network every time theres a change on a server
and my code automatically mails off this alert
to the relavant people, - problem the alerts are
far too frequent - 2 a minute type thing

Here my basic algorithm

When an alert is received

Start a timer,

Any alert you get inbetween,

Check if the exact same alert has been
seen already
if not
        break the timer loop
        and send the mail
if so
        add it to an array of alerts
        and wait until timer is finished

reset timer

end timer loop        

So in effect instead of getting a mail
every 2 minutes about the same problem
the program would would it for a half hour
before mailing it out again ...

Would it be useful to run this as a separate
thread ?

Is there an api in java for these type of problems ?

Any help with this is greatly appreciated ..
0
Comment
Question by:michaelgoc
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
Comment Utility
I don't believe there is any specific help for such things. And no, I don't think a separate timer thread is needed here either. A class with a method that is called for every alert is quite sufficient, based on your description. In pseudo code it would be something like:


class AlertFilter
{   long time;
    AlertList alerts;

    AlertFilter()
    {   time=System.currentTimeMillis();
        alerts=new AlertList();
        return;
    }

    void processAlert(Alert a)
    {   checkTime();
        if(!alerts.has(a))
        {   Send Alert
            alerts.add(a);
        }
        return;
    }

    void checkTime()
    {   if(System.currentTimeMillis()-time>30000)
        {   time=System.currentTimeMillis();
            alerts.reset();
        }
        return;
    }
}


Well, that came out as more real code than pseudo. The details of AlertList and Alert should be easy to imagine.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Have a look at the Timer and TimerTask classes.
They should provide the functionality you are looking for.
0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
Was either answer helpful?

If so, you should now select one, and grade it.

If not, perhaps you could ask a followup question.
0
 

Author Comment

by:michaelgoc
Comment Utility
The basic algorithm was sound
but the check time method was incorrect,
I have written my own timer class and checked
the time elapsed etc with this, ( seems to be working )
Thanks ,

Michael
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countAbc challenge 9 49
firstChar challenge 13 83
Java passing arguments (type error) 2 33
get weblogic logged in user in java 2 39
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now