java—为代码块的执行计时
嗨 我想要一个代码块(函数的某些行),它将运行规定的时间(比如x毫秒)。在java中可以这样做吗?第一种方法:java—为代码块的执行计时,java,Java,嗨 我想要一个代码块(函数的某些行),它将运行规定的时间(比如x毫秒)。在java中可以这样做吗?第一种方法: long startTime = System.nanoTime(); while(System.nanoTime() - startTime < MAX_TIME_IN_NANOSECONDS){ // your code ... } long startTime=System.nanoTime(); while(System.nanoTime()-startTime
long startTime = System.nanoTime();
while(System.nanoTime() - startTime < MAX_TIME_IN_NANOSECONDS){
// your code ...
}
long startTime=System.nanoTime();
while(System.nanoTime()-startTime
第二种方法
在线程中启动代码
睡眠主线只要你需要
终止(停止、中断)线程。根据当前时间戳使用退出条件,或者创建单独的线程并在指定的超时后终止它 您可以使用日历:
timeInMilis = Calendar.getInstance().getTimeInMillis();
while(Calendar.getInstance().getTimeInMilis() - timeInMilis < MAX_TIME_IN_MILIS){
// Execute block
}
timeInMilis=Calendar.getInstance().getTimeinmilis();
while(Calendar.getInstance().getTimeInMilis()-timeInMilis
使用System.currentTimeMillis()
检查时间,并在时间过后退出循环。
long endTime = System.currentTimeMillis() + ClassName.EXECUTION_TIME_MS;
while (System.currentTimeMillis() < endTime) {
// do your stuff
}
如果在循环中运行多个步骤(可能需要很长时间),则应在步骤之间添加额外的检查,如果希望流程在步骤之间中断,则应在指定时间过后退出循环。这是一个设计决策。计时器过期时,是否希望任务完成其正在执行的步骤,然后退出?如何根据期望的结果进行编码取决于您
long endTime = System.currentTimeMillis() + ClassName.EXECUTION_TIME_MS;
while (System.currentTimeMillis() < endTime) {
this.doFirstLongThing();
if (System.currentTimeMillis() >= endTime) break;
this.doSecondLongThing();
if (System.currentTimeMillis() >= endTime) break;
this.doThirdLongThing();
}
long endTime=System.currentTimeMillis()+ClassName.EXECUTION\u TIME\u MS;
while(System.currentTimeMillis()在单独的线程中运行方法,但将其传递给执行器。然后,您可以使用
Future
等待线程完成特定的时间段。如果它没有完成,您将得到一个TimeoutException,然后您可以取消该线程。取消线程会导致线程中断。因此,您的代码必须定期检查线程的中断状态,并在必要时退出
例如:
ExecutorService exec = Executors.newSingleThreadExecutor();
Future<Integer> future = exec.submit(new Callable<Integer>(){
@Override
public Integer call() throws Exception {
//do some stuff
//periodically check if this thread has been interrupted
if (Thread.currentThread().isInterrupted()) {
return -1;
}
//do some more stuff
//check if interrupted
if (Thread.currentThread().isInterrupted()) {
return -1;
}
//... and so on
return 0;
}
});
exec.shutdown();
try {
//wait 5 seconds for the task to complete.
future.get(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
//the task did not complete in 5 seconds
e.printStackTrace();
System.out.println("CANCELLING");
//cancel it
future.cancel(true); //sends interrupt
}
ExecutorService exec=Executors.newSingleThreadExecutor();
Future=exec.submit(new Callable()){
@凌驾
公共整数调用()引发异常{
//做点什么
//定期检查此线程是否已中断
如果(Thread.currentThread().isInterrupted()){
返回-1;
}
//多做点事
//检查是否中断
如果(Thread.currentThread().isInterrupted()){
返回-1;
}
//……等等
返回0;
}
});
exec.shutdown();
试一试{
//等待5秒钟,等待任务完成。
get(5000,时间单位毫秒);
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}捕获(超时异常e){
//任务没有在5秒内完成
e、 printStackTrace();
系统输出打印项次(“取消”);
//取消它
future.cancel(true);//发送中断
}
经过商定的时间后会发生什么?超时?另请参见+1。要么代码本身必须定期停止以检查当前时间,要么另一个线程必须监视时间并发送某种终止信号。是的,谢谢提醒。无论哪种方式,线程都可以被设计成可停止的。
long endTime = System.currentTimeMillis() + ClassName.EXECUTION_TIME_MS;
while (System.currentTimeMillis() < endTime) {
this.doFirstLongThing();
if (System.currentTimeMillis() >= endTime) break;
this.doSecondLongThing();
if (System.currentTimeMillis() >= endTime) break;
this.doThirdLongThing();
}
ExecutorService exec = Executors.newSingleThreadExecutor();
Future<Integer> future = exec.submit(new Callable<Integer>(){
@Override
public Integer call() throws Exception {
//do some stuff
//periodically check if this thread has been interrupted
if (Thread.currentThread().isInterrupted()) {
return -1;
}
//do some more stuff
//check if interrupted
if (Thread.currentThread().isInterrupted()) {
return -1;
}
//... and so on
return 0;
}
});
exec.shutdown();
try {
//wait 5 seconds for the task to complete.
future.get(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
//the task did not complete in 5 seconds
e.printStackTrace();
System.out.println("CANCELLING");
//cancel it
future.cancel(true); //sends interrupt
}