29

私は専門家ではなく、ただの初心者です。ですから、私のためにいくつかのコードを書いてください。

CLASS Aととの 2 つのクラスがある場合CLASS B、内部CLASS Bには という関数がありますfunb()CLASS Aこの関数を10 分おきに呼び出したいと思います。

あなたはすでにいくつかのアイデアを私に与えてくれましたが、私にはよくわかりませんでした。

サンプルコードを投稿していただけますか?

4

5 に答える 5

30

ScheduledExecutorServiceを見てください。

以下は、ScheduledExecutorService を 1 時間 10 秒ごとにビープ音を鳴らすように設定するメソッドを含むクラスです。

 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);
    }
 }
于 2009-08-14T15:09:09.610 に答える
17
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();
    }


}
于 2009-08-14T15:01:51.573 に答える
14

これを試して。設定された分ごとに 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

于 2015-07-09T17:33:10.150 に答える
4
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); 
    }
}
于 2014-08-28T09:19:26.297 に答える