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:io.cloudex.framework.cloud.api.ApiUtils.java

/**
 * Block for the number of provided seconds
 * @param seconds - the number of seconds to block
 *///from   ww w .  j  av  a2  s  .  c o  m
public static void block(int seconds) {
    try {
        TimeUnit.SECONDS.sleep(seconds);

    } catch (InterruptedException e1) {
        log.warn("wait interrupted", e1);
    }
}

From source file:Main.java

/**
 * Creates a single thread executor which ignores all executions that occur
 * while it is busy executing a Runnable. This is useful for tasks that may
 * be requested multiple times from multiple sources, but which only need to
 * take place once.//  w  w  w. j  ava 2s  .c om
 * 
 * @param name the name for threads created within this pool
 */
@SuppressWarnings("serial")
public static ExecutorService newCoalescingThreadPool(String name) {
    return new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),
            newNamedThreadFactory(name)) {
        private boolean executing = false;

        @Override
        public void execute(final Runnable command) {
            synchronized (this) {
                if (executing)
                    return;
                executing = true;
            }
            super.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        command.run();
                    } finally {
                        executing = false;
                    }
                }
            });
        }
    };
}

From source file:Main.java

public static ExecutorService newFixedThreadPool(String name, int numThreads, int maxPoolSize,
        int keepAliveTimeInSeconds) {
    LinkedBlockingQueue<Runnable> lbq = new LinkedBlockingQueue<Runnable>();
    ThreadFactory tf = newNamedThreadFactory(name);
    ThreadPoolExecutor tpe = new ThreadPoolExecutor(numThreads, maxPoolSize, keepAliveTimeInSeconds,
            TimeUnit.SECONDS, lbq, tf);
    return Executors.newFixedThreadPool(numThreads, tpe.getThreadFactory());
}

From source file:org.z.global.util.TimeValue.java

public static TimeValue timeValueSeconds(long seconds) {
    return new TimeValue(seconds, TimeUnit.SECONDS);
}

From source file:ExecutorHttpd.java

public void shutdown() throws InterruptedException {
    executor.shutdown();
    executor.awaitTermination(30, TimeUnit.SECONDS);
    executor.shutdownNow();
}

From source file:Main.java

/**
 * Creates a fixed priority thread pool, sorted with the given comparator.
 *//* w  ww  .j a  v a 2  s  .c  o m*/
public static ExecutorService newPriorityThreadPool(String name, int numThreads,
        Comparator<Runnable> comparator) {
    return new ThreadPoolExecutor(numThreads, numThreads, 1, TimeUnit.SECONDS,
            new PriorityBlockingQueue<Runnable>(numThreads, comparator), newNamedThreadFactory(name));
}

From source file:Main.java

public static ExecutorService fixedThreadsExecutor(String name, int count) {
    ThreadFactory threadFactory = daemonThreadFactory(name);

    return new ThreadPoolExecutor(count, count, 10, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(Integer.MAX_VALUE), threadFactory) {
        @Override//from www  . j  a v  a 2  s.  c o m
        protected void afterExecute(Runnable runnable, Throwable throwable) {
            if (throwable != null) {
                //Console.getInstance().info("Unexpected failure from " + runnable, throwable);
            }
        }
    };
}

From source file:Main.java

public static boolean stop(ExecutorService executorService, int waitBeforeTerminateSecs, Logger logger)
/*     */ {/*from   w w w  .  ja  va  2  s.  c om*/
    /*  53 */int waitMillis = Math.max(1000, 1000 * waitBeforeTerminateSecs);
    /*     */
    /*     */
    /*  56 */executorService.shutdown();
    /*     */
    /*     */
    /*  59 */boolean stopped = false;
    /*  60 */while ((waitMillis > 0) && (!stopped)) {
        /*  61 */long startMillis = System.currentTimeMillis();
        /*     */try {
            /*  63 */logger.debug("Waiting for thread pool to stop");
            /*  64 */stopped = executorService.awaitTermination(waitMillis, TimeUnit.MILLISECONDS);
            /*     */} catch (InterruptedException e) {
            /*  66 */logger.debug("Thread was interrupted while it was waiting for thread pool to stop", e);
            /*  67 */Thread.currentThread().interrupt();
            /*  68 */break;
            /*     */}
        /*  70 */waitMillis = (int) (waitMillis - (System.currentTimeMillis() - startMillis));
        /*     */}
    /*     */
    /*  73 */if (!executorService.isTerminated()) {
        /*  74 */logger.warn("Thread pool will be forcibly stopped now if it has not already stopped");
        /*  75 */executorService.shutdownNow();
        /*     */try {
            /*  77 */stopped = executorService.awaitTermination(waitBeforeTerminateSecs, TimeUnit.SECONDS);
            /*     */}
        /*     */catch (InterruptedException e) {
        }
        /*     */
        /*  81 */if (!executorService.isTerminated()) {
            /*  82 */logger.warn("Could not shutdown thread pool in [{}] seconds",
                    Integer.valueOf(waitBeforeTerminateSecs));
            /*     */}
        /*     */}
    /*     */
    /*  86 */return stopped;
    /*     */}

From source file:ReplaceWorker.java

private static void awaitTermination(ExecutorService threadPool) {
    try {/*from w  ww  .j  av  a  2s  .co  m*/
        threadPool.shutdown();
        boolean awaitTermination = threadPool.awaitTermination(1, TimeUnit.SECONDS);
        System.out.println("terminted successfull: " + awaitTermination);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:me.bramhaag.discordselfbot.util.Util.java

/**
 * Edit message with an error message./*  ww  w .  j  a va  2 s  .  c om*/
 * @param message message to edit.
 * @param reason reason error was thrown.
 */
public static void sendError(@NonNull Message message, @NonNull String reason) {
    message.editMessage(new MessageBuilder().appendCodeBlock("An error occurred! " + reason, "javascript")
            .build().getRawContent(), 5).queue(m -> m.delete().queueAfter(5, TimeUnit.SECONDS));
}