首先如果条件执行,然后调用一个方法,然后我停止控制 3 秒,然后接下来如果条件再次执行,调用一个方法并停止控制 3 秒,同样我执行了 8 个 if 条件。但是我得到了错误的输出。我的输出中发生的情况是,有时在控制停止方法完成之前调用方法。
这是我的编码和输出。给出一个解决方案
package com.example;
import java.util.Timer;
import java.util.TimerTask;
public class TimeBetween {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SecondClass obj = new SecondClass();
int a = 5;
int b = 3;
int c;
System.out.println("Timer Starts");
if (a == 5 && b == 3) {
c = a + b;
System.out.println("-----------Addition:" + c);
obj.secondClassMethod();
timeControlMethod();
}
if (a == 5 && b == 3) {
c = a - b;
System.out.println("-----------Subtraction:" + c);
obj.secondClassMethod();
timeControlMethod();
}
if (a == 5 && b == 3) {
c = a * b;
System.out.println("------------Multipication:" + c);
obj.secondClassMethod();
timeControlMethod();
}
if (a == 5 && b == 3) {
c = a % b;
System.out.println("-------------Modulo:" + c);
obj.secondClassMethod();
timeControlMethod();
}
if (a == 5 && b == 3) {
c = a + b;
System.out.println("************Addition:" + c);
obj.secondClassMethod();
timeControlMethod();
}
if (a == 5 && b == 3) {
c = a - b;
System.out.println("************Subtraction:" + c);
obj.secondClassMethod();
timeControlMethod();
}
if (a == 5 && b == 3) {
c = a * b;
System.out.println("***************Multipication:" + c);
obj.secondClassMethod();
timeControlMethod();
}
if (a == 5 && b == 3) {
c = a % b;
System.out.println("***************Modulo:" + c);
obj.secondClassMethod();
timeControlMethod();
}
System.out.println("Timer Ends");
}
private static void timeControlMethod() {
long getCurrentTimeInMilSec = System.currentTimeMillis();
long setEndTime = getCurrentTimeInMilSec + 3000l;
System.out.println("Time method waiting for 3 sec........");
while (setEndTime > System.currentTimeMillis()) {
}
}
}
class SecondClass {
public void secondClassMethod() {
System.err.println("Inside of SecondClass method");
}
}
输出:
Timer Starts
Inside of SecondClass method
-----------Addition:8
Time method waiting for 3 sec........
-----------Subtraction:2
Time method waiting for 3 sec........
Inside of SecondClass method
------------Multipication:15
Inside of SecondClass method
Time method waiting for 3 sec........
-------------Modulo:2
Time method waiting for 3 sec........
Inside of SecondClass method
************Addition:8
Time method waiting for 3 sec........
Inside of SecondClass method
Inside of SecondClass method************Subtraction:2
Time method waiting for 3 sec........
***************Multipication:15
Time method waiting for 3 sec........
Inside of SecondClass method
***************Modulo:2
Time method waiting for 3 sec........
Inside of SecondClass method
Timer Ends
最佳答案
如果你只是想 sleep 而不做任何事情,那么你可以使用
Thread.sleep(x);
其中 x 是以毫秒为单位的时间。
当人们尝试在多线程环境中使用 Thread.sleep 来修复竞争条件时,Thread.sleep
被认为是不好的。对于这种情况,java wait/notify
模型是更好的选择。但我相信在您的情况下,使用 Thread.sleep 是有效的,因为您并不是试图解决竞争条件,而只是希望执行在所需的时间内停止。
关于java - 如何在java中停止控制几秒钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18755900/