English 中文(简体)
如何运作单30分钟
原标题:How to run java function for only 30 minutes

我需要设立一个支离破碎的职能,只有30分钟,在30分钟后执行。 但是,如果符合适当条件,它也应该能够在规定时间之前自行终止。 我不想睡觉,因为它应该收集数据,因此没有睡觉。

增 编

最佳回答

使用:timer.sstalule ( TimerTask, long )

public void someFunctino() {
    // set the timeout    
    // this will stop this function in 30 minutes
    long in30Minutes = 30 * 60 * 1000;
    Timer timer = new Timer();
    timer.schedule( new TimerTask(){
          public void run() {
               if( conditionsAreMet() ) { 
                   System.exit(0);
                }
           }
     },  in30Minutes );

     // do the work... 
      .... work for n time, it would be stoped in 30 minutes at most
      ... code code code
}
问题回答

用<代码>System. CurrenttimeMillis()计算起步时间,计算现在每次停下来的时间,然后再收集你想要收集的数据。 另一种办法是将时间与数据收集分开,以便每个数据可以自行操作。

如果你能够告诉what你正在收集的数据和how?

与此类似:

long startTime = System.currentTimeMillis();
long maxDurationInMilliseconds = 30 * 60 * 1000;

while (System.currentTimeMillis() < startTime + maxDurationInMilliseconds) {
    // carry on running - 30 minutes hasn t elapsed yet

    if (someOtherConditionIsMet) {
        // stop running early
        break;
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签