0

最初に条件が実行され、次に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
4

1 に答える 1

1

ただ眠りたいだけで何もしない場合は、次を使用できます

Thread.sleep(x);

ここで、x はミリ秒単位の時間です。

Thread.sleepマルチスレッド環境でそれを使用して競合状態を修正しようとすると、悪いと見なされます。このような状況では、Javawait/notifyモデルの方がはるかに適しています。しかし、私はあなたの場合、Thread.sleep競合状態を解決しようとしているのではなく、単に実行を目的の時間停止させたいだけなので、使用するのは有効だと思います。

于 2013-09-12T05:25:55.513 に答える