sleep Random Seconds - Java java.util

Java examples for java.util:Random Int

Description

sleep Random Seconds

Demo Code


//package com.java2s;
import java.util.Random;

public class Main {
    public static void main(String[] argv) throws Exception {
        int bound = 2;
        sleepRandomSeconds(bound);//from  w w  w .  j av  a 2 s .  c o m
    }

    private static final int MS_IN_SECOND = 1000;
    private static final Random RANDOM = new Random();

    public static void sleepRandomSeconds(int bound) {
        sleepSeconds(RANDOM.nextInt(bound));
    }

    public static void sleepSeconds(int seconds) {
        try {
            Thread.sleep(seconds * MS_IN_SECOND);
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
    }
}

Related Tutorials