首先打印一行,然后每30分鍾打印一行

[英]print a line for the firstt time and then every 30 minutes


I have a below method which will be called every one minute from background thread.

我有一個下面的方法,將從后台線程每隔一分鍾調用一次。

public void testMethod() {
    // some other code

    System.out.println("hello")

    // some other code
}

Now every one minute, hello line will get printed out. Is there any way, by which hello line should get printed for the first time and then after every half an hour (it doesn't have to be exact half an hour) once testMethod is invoked by background thread. I don't want to use any executors or timer class.

現在每隔一分鍾,你好行將打印出來。有沒有什么辦法,一旦helme行首次打印,然后每隔半小時打印一次(不一定是半小時),一旦后台線程調用testMethod。我不想使用任何執行程序或計時器類。

I just don't want my hello line gets printed out every one minute. In the starting it should print once and then every 30-35 minutes is fine. Is there any other way to do this without using Timer or Executors? Can we not use current time and do some mod check?

我只是不希望我的hello線每隔一分鍾打印一次。在開始時它應該打印一次,然后每30-35分鍾就可以了。沒有使用Timer或Executors,有沒有其他方法可以做到這一點?我們可以不使用當前時間並進行一些模擬檢查嗎?

I want all other code in testMethod to get executed every one minute so I cannot sleep or anything. I just need to have some random check by which we can decide whether to print hello or not. I just don't want to print hello every one minute.

我希望testMethod中的所有其他代碼每隔一分鍾執行一次,這樣我就無法入睡或任何事情。我只需要進行一些隨機檢查,我們可以決定是否打印你好。我只是不想每隔一分鍾打印你好。

3 个解决方案

#1


3  

Have a static member variable for stoping the loop

有一個靜態成員變量來停止循環

private static boolean isTrue = true;

public static boolean getIsTrue() {
    return isTrue;
}

public static void setIsTrue(boolean test) {
    YourClass.isTrue = test;
}

Your program should be

你的程序應該是

    System.out.println("hello");
    Thread printText = new Thread(){
        public void run() {
            while(getIsTrue()) {
                try {
                    Thread.sleep(30*60*1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("hello");
            }
        }
    };
    printText.start();

When you want to stop your loop

當你想要停止你的循環

public void testMethod() {
    setIsTrue(false);
}

#2


0  

As you said you are calling it from a background thread you can just use an infinite loop or a finite loop with a Thread.sleep(30*60*1000) into it

如你所說,你是從后台線程調用它,你可以使用無限循環或帶有Thread.sleep(30 * 60 * 1000)的有限循環

If you want it to loop infinitely

如果你想讓它無限循環

public void testMethod() {
    (new Thread() {
        public void run() {
            long unixTime = System.currentTimeMillis() / 1000L;
            while(true){
                long currentUnixTime = System.currentTimeMillis() / 1000L;
                if ((currentUnixTime - unixTime)>=30*60){
                    System.out.println("hello");
                    unixTime = currentUnixTime;
                }
            }
        }
   }).start();
}

This implementation will waste a lot a your cpu cycle, you can use the same logic to implement a finite one.

這種實現會浪費很多你的cpu周期,你可以使用相同的邏輯來實現一個有限的。

#3


0  

Seems to me the Question is not asking about sleeping for a half hour. The Question says that part of a method should be skipped unless at least a half hour has elapsed.

在我看來,問題並不是要求睡半小時。問題說,除非至少半小時過去,否則應該跳過部分方法。

In Java 8 and later you should be using the java.time framework for date-time work. All we need is the Instant class which represents a moment on the timeline in UTC.

在Java 8及更高版本中,您應該使用java.time框架進行日期時間工作。我們所需要的只是Instant類,它代表UTC時間軸上的一個時刻。

Store the next time the extra code needs to be executed in a variable of type java.time.Instant. Initialize to a moment ago so the extra code will run on first time through.

下次需要在java.time.Instant類型的變量中執行額外代碼時存儲。剛剛初始化,所以額外的代碼將在第一次運行。

Instant instantWhenDue = Instant.now().minusSeconds( 1 );

In your method add a test to see if the current moment is past our due time.

在您的方法中添加測試以查看當前時刻是否超過了我們的到期時間。

// do some stuff…
if ( Instant.now().isAfter( instantWhenDue ) ) {
    // do your extra work like say Hello…
    // Set when due to run again.
    instantWhenDue = Instant.now().plusMinutes( 30 );
}
// do other stuff…

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/02/05/724df81068f5e0404bdc92cd843201d.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com