0

首先如果条件执行,然后调用一个方法,然后我停止控制 3 秒,然后如果条件再次执行一个方法并停止控制 3 秒,同样我执行 8 个如果条件。但我得到错误的输出。我的输出中发生的情况是,有时在控制停止方法完成之前调用方法。

这是我的编码和输出。给出一个解决方案

                  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 回答