java - 定时器事件是否同步?

标签 java multithreading timer

我有两个计时器 tm1tm2 以及一个计时器事件处理程序 ts。我能否确定函数 onTimeout() 只会被一个计时器调用(第二个计时器将等待 onTimout() 退出)?如果没有 - 如何实现这一目标?我必须使用 Java 1.4 来完成此任务。

package thr;





import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

interface iTimerSupporter
{
    public void onTimeout();
}

class TimerSupporter implements iTimerSupporter 
{
    public void onTimeout()
    {
        System.out.println("time");     
    }   
}

public class TimeMachine  
{

    public static int TimerCount = 0;
    public  final int TimeoutValue = 5;
    public static int TimerID = 0;

    iTimerSupporter EventPrenumerator;

    private String info;
    static Timer timer = null;

    private String getDateTime(Date date)
    {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        return dateFormat.format(date);
    }

    public class notif extends TimerTask
    {
        String info;
        Date started = new Date();
        Date stoped = null;


        public notif(String info)
        {
            this.info = info;
        }

        public void run()
        {
            if (EventPrenumerator != null)
                {
                    try
                    {
                    stoped = new Date();
                    System.out.println("main timeout "  + " started: " + getDateTime(started) + " stopped: "
                            + getDateTime(stoped) + " timeout ID=" + info );
                    EventPrenumerator.onTimeout();
                    }catch ( Exception e)
                    {
                        System.out.println("error in timeout event "+e.getMessage() );

                    }
                }
            timer.cancel();

        }
    }





    public void startTimer(String info)
    {
        TimerCount++;
        System.out.println("start . Will wait " + TimeoutValue + " timeout ID="+info  );
        timer = new Timer();
        notif n = new notif(info);
        this.info=info;
        timer.schedule(n, TimeoutValue * 1000);
    }




    public void startTimer()
    {

        info= Integer.toString (TimerID++);
        startTimer(info);
    }   



    public String state()
    {
        return "TimerCount=" + TimerCount+ " timeout ID="+info;
    }





    private void stopTimer()
    {
            TimerCount--;
            timer.cancel();
    }

    public static void main(String[] value)
    {
        TimerSupporter ts = new TimerSupporter();
        TimeMachine tm1 = new TimeMachine();
        tm1.EventPrenumerator = ts;
        TimeMachine tm2 = new TimeMachine();
        tm2.EventPrenumerator = ts;

        tm1.startTimer();
        tm2.startTimer();

        System.out.println("exit");

    }



}

最佳答案

每个计时器在它自己的线程中运行任务。因此,您可以确保两个计时器能够同时调用此方法,并且它们不会等待其他计时器任务完成。

您应该使目标方法同步以防止并发执行。还有许多其他方法可以帮助您实现目标。 我建议阅读this Oracle tutorial关于并发。

关于java - 定时器事件是否同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35505121/

相关文章:

java - Firebase MLKIT 翻译 (firebase-ml-natural-language-translate-model) 的依赖性错误

java - 指定 Maven 分类器的依赖关系

java - 当我在可绘制文件夹中导入图像并尝试在 ImageView 上使用图像时,应用程序崩溃。但是,当我设置图像时,应用程序工作正常

c++ - Afxbeginthread 和 CreateThread 的区别

在 C 中计算时间跨度

c++ - Qt QLCDNumber问题

具有多个超时时间的 Java 定时器

java - localsettings.properties : How to use its properties for JTest configuration?

C# - 是否可以迭代 GC 堆?

c# - CultureInfo线程安全