All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.java.truecommons.shed.Threads Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
/*
 * Copyright (C) 2005-2012 Schlichtherle IT Services.
 * All rights reserved. Use is subject to license terms.
 */
package net.java.truecommons.shed;

import javax.annotation.concurrent.Immutable;

/**
 * Static utility methods for {@link Thread}s.
 * 
 * @author Christian Schlichtherle
 */
@Immutable
public class Threads {

    private Threads() { }

    /**
     * Uninterruptibly pauses the current thread for the given time interval.
     * 
     * @param  millis the milliseconds to pause the current thread.
     * @throws IllegalArgumentException if {@code millis} is negative.
     */
    @SuppressWarnings("SleepWhileInLoop")
    public static void pause(long millis) {
        final long start = System.currentTimeMillis();
        boolean interrupted = false;
        do {
            try {
                Thread.sleep(millis);
            } catch (InterruptedException interrupt) {
                interrupted = true;
            }
        } while (0 < (millis -= System.currentTimeMillis() - start));
        if (interrupted) Thread.currentThread().interrupt(); // restore
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy