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

时间:2013-09-12 05:20:39

标签: java controls

首先如果条件执行然后一个方法被调用然后我停止控制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

1 个答案:

答案 0 :(得分:2)

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

Thread.sleep(x);

其中x是以毫秒为单位的时间。

当人们尝试在多线程环境中使用它来修复竞争条件时,

Thread.sleep被认为是错误的。对于这样的坐标,java wait/notify模型是一个更好的选择。但我相信你的情况,使用Thread.sleep是有效的,因为你不是要解决竞争条件而只是希望你的执行停止一段时间。