最初に条件が実行され、次に1つのメソッドが呼び出された場合、次に3秒間コントロールを停止し、次に条件が再度実行された場合、1つのメソッドが呼び出され、コントロールを3秒間停止します。私の出力で起こったことは、制御停止メソッドの完了前にメソッドが呼び出されることがあるということです。
ここに私のコーディングと出力があります。1つの解決策を与える
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