简体   繁体   中英

How to create a java utility class for time sleep

i want to create an utility class for time sleep in java with predefined time which i want to call in another call.

for example actually i have: my utility class:

public static void waitFor(int sec) {
        try {
            Thread.sleep(sec * 1000L);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

With this class i can the method Utility.waitFor(20); in another class and it works perfectly.

What i want is to initiate the time in Utiliy class for example for 20s and call the method Utility.waitFor(); I don't want to put the time inside the bracket in my second class.

Because in the future if i want to modify the time i will not have to go through all the code and change the time. If i fix the time in utility class, this will work in everywhere

Thank you for help

In your utility class add a static int that you can set:

class Utility
{
    private static int sleepTime = 20;
    public static void setSleepTime( int time ){ sleepTime = time; }

    public static void waitFor(){
       try {
          Thread.sleep(sleepTime * 1000L);
       } catch (Exception e) {
          System.out.println(e.toString());
       }
    }
}

Then elsewhere you just call Utility.setSleepTime() when you want to change the length of time it waits.

Because in the future if i want to modify the time i will not have to go through all the code and change the time. If i fix the time in utility class, this will work in everywhere.

That's completely contrast statements and the way you are doing now is the proper option.

1)If all the time is same, you just put in method directly. 2) Otherwise , you have to pass the time.

No other options and won't make sense even, at least for me :)

What I suggest is just maintain constants to pass as argument and you just need to edit that constants each time.

For ex:

public static final long TIME_FOR_THAT = 10;
public static final long TIME_FOR_THIS = 20;

And while calling

setSleepTime(Timings.TIME_FOR_THAT);

See, everything is at one place. Just edit your times in one file. And even you can externalize it by properties file even to maintain it from configuration side :)

Why not create pass the the time you want to wait to the class and allow yourself to change it later, if need be.

public class Utility {
     int secsToWait;

      public Utility(int secsToWait) {
           this.secsToWait = secsToWait;
      }

     // So you can change it later, after creation, if you need to
     public void setTime(int seconds) {
          secsToWait = seconds;
     }


     public void wait() {
          try {
               Thread.sleep(secsToWait);
          } catch (Exception e) {
               System.out.println(e.toString());
          }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM