Example usage for java.util.concurrent TimeUnit SECONDS

List of usage examples for java.util.concurrent TimeUnit SECONDS

Introduction

In this page you can find the example usage for java.util.concurrent TimeUnit SECONDS.

Prototype

TimeUnit SECONDS

To view the source code for java.util.concurrent TimeUnit SECONDS.

Click Source Link

Document

Time unit representing one second.

Usage

From source file:Main.java

/**
 * Tis method provides the UNIX-time divided by TIME_STEP in milliseconds
 * //from   www.j a  v  a  2s  .c  o m
 * @return   the UNIX-time divided by TIME_STEP in milliseconds
 */
public static long getUnixTimeDividedByTimeStep() {
    return (new Date().getTime() / TimeUnit.SECONDS.toMillis(TIME_STEP));
}

From source file:Main.java

/**
 * @param uptime has to be in milliseconds
 * @return String representation rounded down to seconds, minutes, hours or days
 *//*ww  w .  j av  a 2s.c o m*/
@NonNull
public static String getTimeString(int uptime) {
    int uptimeSecs = uptime / 10;
    String timeOn;
    if (TimeUnit.SECONDS.toMinutes(uptimeSecs) == 0) {
        timeOn = uptimeSecs + (uptimeSecs == 1 ? " sec" : " secs");
    } else if (TimeUnit.SECONDS.toHours(uptimeSecs) == 0) {
        long uptimeMins = TimeUnit.SECONDS.toMinutes(uptimeSecs);
        timeOn = uptimeMins + (uptimeMins == 1 ? " min" : " mins");
    } else if (TimeUnit.SECONDS.toDays(uptimeSecs) == 0) {
        long uptimeHours = TimeUnit.SECONDS.toHours(uptimeSecs);
        timeOn = uptimeHours + (uptimeHours == 1 ? " hour" : " hours");
    } else {
        long uptimeDays = TimeUnit.SECONDS.toDays(uptimeSecs);
        timeOn = uptimeDays + (uptimeDays == 1 ? " day" : " days");
    }
    return timeOn;
}

From source file:Main.java

public static void newScheduledThreadPoolAtFixedRate(Runnable runnable, long delayTime, long period, int size) {
    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(size);
    scheduledThreadPool.scheduleAtFixedRate(runnable, delayTime, period, TimeUnit.SECONDS);
}

From source file:Main.java

public static void await(CountDownLatch latch, int timeout) {
    try {// w  w w  . jav a 2s .  c  o m
        latch.await(timeout, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        // Meh
    }
}

From source file:Main.java

public static boolean shutdown() {
    try {//  w  w w.  j  av  a2  s  . co m
        executorService.shutdown();
        return executorService.awaitTermination(STANDARD_THREAD_COUNT / 100, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
        return true;
    } finally {
        executorService = Executors.newFixedThreadPool(6);
    }
}

From source file:Main.java

/***
 * //  w w w .j  a v  a2  s  .c  o m
 * @param runnable
 * @param delayTime
 * @param size
 */
public static void newScheduedThreadPool(Runnable runnable, long delayTime, int size) {
    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(size);
    scheduledThreadPool.schedule(runnable, delayTime, TimeUnit.SECONDS);
}

From source file:Main.java

public static TimeUnit convert(ChronoUnit tu) {
    if (tu == null) {
        return null;
    }//  w ww .  j  ava2s .c  o  m
    switch (tu) {
    case DAYS:
        return TimeUnit.DAYS;
    case HOURS:
        return TimeUnit.HOURS;
    case MINUTES:
        return TimeUnit.MINUTES;
    case SECONDS:
        return TimeUnit.SECONDS;
    case MICROS:
        return TimeUnit.MICROSECONDS;
    case MILLIS:
        return TimeUnit.MILLISECONDS;
    case NANOS:
        return TimeUnit.NANOSECONDS;
    default:
        //TODO support the rest
        throw new UnsupportedOperationException("Man, use a real temporal unit");
    }
}

From source file:Main.java

public static void runRunnablesNTimesAndAwaitCompletion(int times, final List<Runnable> runnables)
        throws InterruptedException {
    ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(runnables.size());
    for (Runnable r : runnables) {
        for (int i = 0; i < times; i++) {
            newFixedThreadPool.execute(r);
        }//w w w  .ja v  a 2  s .  c om
    }
    newFixedThreadPool.shutdown();
    newFixedThreadPool.awaitTermination(5, TimeUnit.SECONDS);
}

From source file:Main.java

public static ThreadPoolExecutor newThreadPool(int maximumNumOfThreads, String poolName) {
    BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(CAPACITY);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maximumNumOfThreads, maximumNumOfThreads, 1,
            TimeUnit.SECONDS, workQueue, createFactory(poolName));
    threadPoolExecutor.allowCoreThreadTimeOut(true);
    return threadPoolExecutor;
}

From source file:Main.java

private static void doSomething(Trade t) {
    try {/*from ww  w  .j  a v a 2s. c o m*/
        System.out.println(t);
        TimeUnit.SECONDS.sleep(2);
    } catch (InterruptedException ex) {
        System.out.println("Sleeping...");
    }
}