每10分钟调用一次函数 [英] Calling a function every 10 minutes
本文介绍了每10分钟调用一次函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不是专家,只是初学者。所以我恳请你为我写一些代码。
I'm not an expert, just a beginner. So I kindly ask that you write some code for me.
如果我有两个班级, CLASS A
和 CLASS B
,并且在 CLASS B
里面有一个名为的函数funb()
。我想每隔十分钟从 CLASS A
调用此函数。
If I have two classes, CLASS A
and CLASS B
, and inside CLASS B
there is a function called funb()
. I want to call this function from CLASS A
every ten minutes.
你已经给了我一些想法,不过我不太明白。
You have already given me some ideas, however I didn't quite understand.
你能发布一些示例代码吗?
Can you post some example code, please?
推荐答案
这是一个类,其方法设置ScheduledExecutorService每隔十秒钟发出一小时的哔声:
Here is a class with a method that sets up a ScheduledExecutorService to beep every ten seconds for an hour:
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}
这篇关于每10分钟调用一次函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文