Solved

Java Delay/Timer Question

Posted on 2001-09-06
4
200 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
ID: 6461007
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
ID: 6462493
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
ID: 6464404
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
ID: 6470317
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PDFBox - convert image to PDF, PDF resolution 3 118
groovy example issue 10 90
Java JRE greater than 1.6 5 50
Error trying to install RTMT Win7 5 41
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

895 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

15 Experts available now in Live!

Get 1:1 Help Now