如何在java中解决这个Timer问题? [英] How to solve this Timer issue in java?

查看:28
本文介绍了如何在java中解决这个Timer问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Timer 开发 java 电子邮件应用程序,我有两个名为 ActiveProcesses、InActiveProcesses 的数组列表.如果我启动计时器,它将每秒钟发送一封带有 InActiveProcesses 列表值的电子邮件.但问题是如果 InActiveProcesses 列表值相同,计时器会发送电子邮件.例如,InActiveProcess 列表包含值 abcd,它将每隔几秒发送具有相同列表值的电子邮件.我只想发送包含不同值的 InActiveProcesses 列表的电子邮件.计时器将每秒钟检查一次值,如果值不同,它将发送电子邮件.如何使用java处理这个问题.提前致谢.这是代码,

Im developing the java Email application with Timer, I have a two arraylists named ActiveProcesses,InActiveProcesses. If I start the Timer it will send the Email with InActiveProcesses list values for every seconds. But the problem is the timer is sent the Email if the InActiveProcesses list values is same.For Example the InActiveProcess list contains the value abcd, it will send Email every seconds with same List values. I want to send the Email only the InActiveProcesses list contains different values. The Timer will check the values every seconds if the values are different it will send the Email. How to handle this problem using java. Thanks in advance. Here is the code,

for (int i = 0; i < InActiveProcess.size(); i++)
{
    if (!ActiveProcess.contains(InActiveProcess.get(i)))
    {
        list3.add(InActiveProcess.get(i));
    }
}
for (int i = 0; i < ActiveProcess.size(); i++)
{
    if (!InActiveProcess.contains(ActiveProcess.get(i)))
    {
        list3.add(ActiveProcess.get(i));
    }
}
log.info("Processes which are Not Running: " + list3);

StringBuilder sb = new StringBuilder();

for (int k = 0; k < list3.size(); k++)
{

    Result = list3.get(k);

    sb.append(Result.toString());

    sb.append(" ");

}

String message = sb.toString();

log.info(message);

sms.SendMessage("1254554555", message);

es.SendMail("abcdr@gmail.com", " Server process is down", message);

这是我的计时器课程.

int delay = 5000; // delay for 5 sec.

int interval = 1000; // iterate every sec.

Timer timer = new Timer();

        timer.scheduleAtFixedRate(new sample() {

        }, delay, interval);

定时器是执行 sample() 类每隔几秒并将电子邮件发送到指定的地址.我想处理 Timer will Execute for the same time, the Email is sent es.SendMail("abcdr@gmail.com", " Server process is down", message); ifmessage 包含的值不同.

The Timer is Execute the sample() class for Every seconds and sent Email to specified Address. I want to handle the Timer will Execute for every second at the same time, the Email is sent es.SendMail("abcdr@gmail.com", " Server process is down", message); if message contains values is different.

推荐答案

更新:发布此答案后问题发生了变化.这是原始问题的答案:

Update: The question changed after posting this answer. This is the answer to the original question:

您可以使用 计时器 类.

您可以通过 计时器任务.这个类是抽象的,所以你必须扩展它并实现方法 run().

You can schedule actions to be taken when the timer is off through a TimerTask. This class is abstract so you have to extend it and implement the method run().

class MyTimerTask extends TimerTask {

   @Override
   public void run(){
   // Task to do here
 }
}

然后在你的主班

MyTimerTask task = new MyTimerTask(); // this class implements WHAT to do inside the method run();
Date date = new Date(...) // This class will tell timer WHEN to do the task
Timer timer = new Timer(task,date); //Good to go!

或者,您可以执行 Timer timer = new Timer(task,delay) 以在 delay 毫秒后执行任务

Alternatively you can do Timer timer = new Timer(task,delay) to have the task executed after delay miliseconds

这篇关于如何在java中解决这个Timer问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆