Java中的C#中的计时器等效? [英] Equivalent of Timer in C# in Java?

查看:107
本文介绍了Java中的C#中的计时器等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,启用时,定时器将以特定间隔触发事件。我如何在Java中实现这一点?

In C#, the timer will trigger an event at a specific interval when enabled. How do I achieve this in Java?

我想让一个方法在特定的间隔运行。

I want to make a method to be run at a specific interval. I know how to do this in C#, but not Java.

在C#中的代码:

private void timer1_Tick(object sender, EventArgs e)
{
    //the method
}

我尝试了 Timer TimerTask ,但我不确定该方法是否会在其他方法运行时运行。

I tried Timer and TimerTask, but I am not sure whether the method will run when other methods are running.

推荐答案

您正在查找正确的类。 计时器 TimerTask 是正确的,如果你使用它们,它们将在后台运行:

You are looking at the right classes. The Timer and TimerTask are the right ones, and they will run in the background if you use them something like this:

TimerTask task = new RunMeTask();
Timer timer = new Timer();
timer.schedule(task, 1000, 60000);

这篇关于Java中的C#中的计时器等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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