我需要设立一个支离破碎的职能,只有30分钟,在30分钟后执行。 但是,如果符合适当条件,它也应该能够在规定时间之前自行终止。 我不想睡觉,因为它应该收集数据,因此没有睡觉。
增 编
我需要设立一个支离破碎的职能,只有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;
}
}
现代<代码>java.util.con 当前将使用 rel=“nofollow”Executorservice
。 有若干次“<代码>invoke”方法需要时间。
这里的例子有:
public static void main(String args[]) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.invokeAll(Arrays.asList(new Task()), 30, TimeUnit.MINUTES);
executor.shutdown();
}
www.un.org/spanish/ecosoc 参看:
public class Task implements Callable<String> {
@Override
public String call() {
// Just a dummy long running task.
BigInteger i = new BigInteger("0");
for (long l = 0; l < Long.MAX_VALUE; l++) {
i.multiply(new BigInteger(String.valueOf(l)));
// You need to check this regularly..
if (Thread.interrupted()) {
System.out.println("Task interrupted!");
break; // ..and stop the task whenever Thread is interrupted.
}
}
return null; // Or whatever you d like to use as return value.
}
}
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...