每 10 分钟调用一次函数

发布于 2024-07-30 09:44:32 字数 252 浏览 5 评论 0原文

我不是专家,只是初学者。 所以我恳请你为我写一些代码。

如果我有两个类,CLASS ACLASS B,并且在 CLASS B 内部有一个名为 funb() 的函数代码>. 我想每十分钟从CLASS A调用这个函数。

你已经给了我一些想法,但我不太明白。

您可以发布一些示例代码吗?

I'm not an expert, just a beginner. So I kindly ask that you write some code for me.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

∝单色的世界 2024-08-06 09:44:32

看一下 ScheduledExecutorService

下面是一个类,其中的方法设置 ScheduledExecutorService 在一个小时内每十秒发出一次蜂鸣声:

 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);
    }
 }

Have a look at the 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);
    }
 }
公布 2024-08-06 09:44:32
import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

public class ClassExecutingTask {

    long delay = 10 * 1000; // delay in milliseconds
    LoopTask task = new LoopTask();
    Timer timer = new Timer("TaskName");

    public void start() {
        timer.cancel();
        timer = new Timer("TaskName");
        Date executionDate = new Date(); // no params = now
        timer.scheduleAtFixedRate(task, executionDate, delay);
    }

    private class LoopTask extends TimerTask {
        public void run() {
            System.out.println("This message will print every 10 seconds.");
        }
    }

    public static void main(String[] args) {
        ClassExecutingTask executingTask = new ClassExecutingTask();
        executingTask.start();
    }


}
import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

public class ClassExecutingTask {

    long delay = 10 * 1000; // delay in milliseconds
    LoopTask task = new LoopTask();
    Timer timer = new Timer("TaskName");

    public void start() {
        timer.cancel();
        timer = new Timer("TaskName");
        Date executionDate = new Date(); // no params = now
        timer.scheduleAtFixedRate(task, executionDate, delay);
    }

    private class LoopTask extends TimerTask {
        public void run() {
            System.out.println("This message will print every 10 seconds.");
        }
    }

    public static void main(String[] args) {
        ClassExecutingTask executingTask = new ClassExecutingTask();
        executingTask.start();
    }


}
洛阳烟雨空心柳 2024-08-06 09:44:32

尝试这个。 它将每隔设定的分钟重复一次 run() 函数。 要更改设置的分钟,请更改 MINUTES 变量

int MINUTES = 10; // The delay in minutes
Timer timer = new Timer();
 timer.schedule(new TimerTask() {
    @Override
    public void run() { // Function runs every MINUTES minutes.
        // Run the code you want here
        CLASSB.funcb(); // If the function you wanted was static
    }
 }, 0, 1000 * 60 * MINUTES);
    // 1000 milliseconds in a second * 60 per minute * the MINUTES variable. 

不要忘记进行导入!

import java.util.Timer;
import java.util.TimerTask;

欲了解更多信息,请访问此处:

http://docs.oracle.com/ javase/7/docs/api/java/util/Timer.html
http://docs.oracle.com/javase/7 /docs/api/java/util/TimerTask.html

Try this. It will repeat the run() function every set minutes. To change the set minutes, change the MINUTES variable

int MINUTES = 10; // The delay in minutes
Timer timer = new Timer();
 timer.schedule(new TimerTask() {
    @Override
    public void run() { // Function runs every MINUTES minutes.
        // Run the code you want here
        CLASSB.funcb(); // If the function you wanted was static
    }
 }, 0, 1000 * 60 * MINUTES);
    // 1000 milliseconds in a second * 60 per minute * the MINUTES variable. 

Don't forget to do the imports!

import java.util.Timer;
import java.util.TimerTask;

For more info, go here:

http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html
http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html

谈情不如逗狗 2024-08-06 09:44:32
public class datetime {

    public String CurrentDate() {

        java.util.Date dt = new java.util.Date();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        String currentTime = sdf.format(dt);
        return currentTime;

    }

    public static void main(String[] args) {
        class SayHello extends TimerTask {

            datetime thisObj = new datetime();

            public void run() {
                String todaysdate = thisObj.CurrentDate();
                System.out.println(todaysdate);
            }
        }
        Timer timer = new Timer();
        timer.schedule(new SayHello(), 0, 5000); 
    }
}
public class datetime {

    public String CurrentDate() {

        java.util.Date dt = new java.util.Date();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        String currentTime = sdf.format(dt);
        return currentTime;

    }

    public static void main(String[] args) {
        class SayHello extends TimerTask {

            datetime thisObj = new datetime();

            public void run() {
                String todaysdate = thisObj.CurrentDate();
                System.out.println(todaysdate);
            }
        }
        Timer timer = new Timer();
        timer.schedule(new SayHello(), 0, 5000); 
    }
}
娇女薄笑 2024-08-06 09:44:32

Java 8 的解决方案

ClassB b = new ClassB();    
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> {
    b.funb();
};
executor.scheduleWithFixedDelay(task, 0, 10, TimeUnit.MINUTES);

Solution with Java 8

ClassB b = new ClassB();    
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> {
    b.funb();
};
executor.scheduleWithFixedDelay(task, 0, 10, TimeUnit.MINUTES);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文