如何在java中停止控制几秒钟?

如何在java中停止控制几秒钟?,java,controls,Java,Controls,首先执行if条件,然后调用一个方法,然后我停止控制3秒钟,然后再次执行if条件,调用一个方法并停止控制3秒钟,同样,我执行了8个if条件。但是我得到了错误的输出。在我的输出中发生的是,有时在控制停止方法完成之前调用方法 这里是我的编码和输出。给出一个解决方案 package com.example; import java.util.Timer; import java.util.TimerT

首先执行if条件,然后调用一个方法,然后我停止控制3秒钟,然后再次执行if条件,调用一个方法并停止控制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

如果你只是想睡觉,什么也不做,那么你可以使用

Thread.sleep(x);
其中x是以毫秒为单位的时间


Thread.sleep
当人们试图在多线程环境中使用它来修复竞争条件时,它被认为是不好的。对于这种情况,java
wait/notify
模型是一个更好的选择。但我相信你的情况,使用
Thread.sleep是有效的,因为您不想解决争用条件,只想让执行在所需的时间内停止。

我建议您尝试利用Java线程等待和通知机制。.使用线程睡眠方法不是一个好的解决方案。我试着运行了4次代码,每次我得到一致的输出,其中打印到控制台的文本顺序与源代码上的调用顺序一致。此外,我没有得到任何缩进,因为你在你的输出上面。您是如何运行程序的?我通过EclipseIDE运行程序