List of usage examples for java.util.concurrent TimeUnit toMillis
public long toMillis(long duration)
From source file:me.j360.dubbo.modules.util.concurrent.ThreadUtil.java
/** * sleep???InterruptedException./*from w ww. j a v a 2s . c o m*/ */ public static void sleep(long duration, TimeUnit unit) { try { Thread.sleep(unit.toMillis(duration)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }
From source file:io.selendroid.standalone.server.util.HttpClientUtil.java
public static void waitForServer(int port, long timeout, TimeUnit timeoutUnit) { long end = System.currentTimeMillis() + timeoutUnit.toMillis(timeout); while (!isServerStarted(port)) { if (System.currentTimeMillis() > end) { throw new SelendroidException(String .format("Selendroid standalone server failed to start within %d %s", timeout, timeoutUnit)); }/*from w ww . ja v a 2 s. co m*/ try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }
From source file:ch.ivyteam.ivy.maven.engine.deploy.MarkerFileDeployer.java
private static void wait(Supplier<Boolean> condition, long duration, TimeUnit unit) throws TimeoutException { long waitMs = unit.toMillis(duration); long startMs = System.currentTimeMillis(); long maxMs = waitMs + startMs; while (!condition.get()) { try {//from www . j a va2 s . c om if (System.currentTimeMillis() > maxMs) { throw new TimeoutException("Operation reached timeout of " + duration + " " + unit); } Thread.sleep(100); } catch (InterruptedException ex) { } } }
From source file:com.codeabovelab.dm.common.utils.ProcessUtils.java
public static ExecuteWatchdog createTimeoutWatchdog(TimeUnit timeunit, int timeout) { ExecuteWatchdog timeoutWatchdog = new ExecuteWatchdog(timeunit.toMillis(timeout)); return timeoutWatchdog; }
From source file:Main.java
public static final void sleep(long pTime, TimeUnit pTimeUnit) { final long lStart = System.nanoTime(); long lDeadlineInNanos = lStart + pTimeUnit.toNanos(pTime); boolean lSleepTimeBelowMillisecond = pTimeUnit.toMillis(pTime) == 0; long lNanoTime; while ((lNanoTime = System.nanoTime()) < lDeadlineInNanos) { try {//from www .j a v a 2 s . c o m if (lSleepTimeBelowMillisecond) { long lTimeToWaitInNanos = 3 * (lDeadlineInNanos - lNanoTime) / 4; if (lTimeToWaitInNanos > 0) Thread.sleep(0, (int) lTimeToWaitInNanos); } else { long lTimeToWaitInNanos = 3 * (lDeadlineInNanos - lNanoTime) / 4; if (lTimeToWaitInNanos > 0) { long lTimeToWaitInMillis = TimeUnit.NANOSECONDS.toMillis(lTimeToWaitInNanos); Thread.sleep(lTimeToWaitInMillis, (int) (lTimeToWaitInNanos % 1000000L)); } } } catch (InterruptedException e) { } } }
From source file:com.indoqa.lang.util.TimeUtils.java
/** * Formats the given amount of time to represent seconds and milliseconds.<br> * <br>//from w w w . j av a2 s . c o m * The result's format will be <code>#,##0.000</code>. * * @param amount The amount of time expressed in <code>timeUnit</code>. * @param timeUnit The unit of <code>amount</code> * * @return The formatted result. * * @see DecimalFormat * @see TimeUnit */ public static String formatSecondsAndMilliseconds(long amount, TimeUnit timeUnit) { double milliseconds = timeUnit.toMillis(amount); double seconds = milliseconds / MILLIS_PER_SECOND; return new DecimalFormat("#,##0.000").format(seconds); }
From source file:com.dcsquare.hivemq.spi.config.Configurations.java
/** * Creates a new Properties file configuration which automatically reloads itself in the given interval. * <p/>/*from www .j av a 2 s .c o m*/ * Searches for the configuration file in the plugins folder of HiveMQ. * * @param fileName the file name of the properties file which is located in the plugins folder of HiveMQ. * @param reReadSchedule the time schedule when a reload should occur * @param timeUnit the time unit of the reReadSchedule * @return a reloadable properties file configuration */ public static AbstractConfiguration newReloadablePropertiesConfiguration(final String fileName, final int reReadSchedule, final TimeUnit timeUnit) { final PropertiesFileDatasource datasource = new PropertiesFileDatasource(PathUtils.getPluginFolder(), fileName); final Long scheduleInMillis = timeUnit.toMillis(reReadSchedule); final AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(scheduleInMillis.intValue(), scheduleInMillis.intValue(), true); return new DynamicConfiguration(datasource, scheduler); }
From source file:com.google.cloud.runtime.jetty.util.HttpUrlUtil.java
/** * Wait for a server to be "up" by requesting a specific GET resource * that should be returned in status code 200. * <p>/*from w w w.j av a 2s. c om*/ * This will attempt a check for server up. * If any result other then response code 200 occurs, then * a 2s delay is performed until the next test. * Up to the duration/timeunit specified. * </p> * * @param uri the URI to request * @param duration the time duration to wait for server up * @param unit the time unit to wait for server up */ public static void waitForServerUp(URI uri, int duration, TimeUnit unit) { System.err.println("Waiting for server up: " + uri); boolean waiting = true; long expiration = System.currentTimeMillis() + unit.toMillis(duration); while (waiting && System.currentTimeMillis() < expiration) { try { System.out.print("."); HttpURLConnection http = openTo(uri); int statusCode = http.getResponseCode(); if (statusCode != HttpURLConnection.HTTP_OK) { log.log(Level.FINER, "Waiting 2s for next attempt"); TimeUnit.SECONDS.sleep(2); } else { waiting = false; } } catch (MalformedURLException e) { throw new IllegalArgumentException("Invalid URI: " + uri.toString()); } catch (IOException e) { log.log(Level.FINEST, "Ignoring IOException", e); } catch (InterruptedException ignore) { // ignore } } System.err.println(); System.err.println("Server seems to be up."); }
From source file:tachyon.client.file.FileSystemUtils.java
/** * Wait for a file to be marked as completed. * <p/>/*from ww w . ja v a 2s . co m*/ * The calling thread will block for <i>at most</i> {@code timeout} time units (as specified via * {@code tunit} or until the file is reported as complete by the master. The method will return * the last known completion status of the file (hence, false only if the method has timed out). A * zero value on the {@code timeout} parameter will make the calling thread check once and return; * a negative value will make it block indefinitely. Note that, in this last case, if a file is * never completed, the thread will block forever, so use with care. * <p/> * Note that the file whose uri is specified, might not exist at the moment this method this call. * The method will deliberately block anyway for the specified amount of time, waiting for the * file to be created and eventually completed. Note also that the file might be moved or deleted * while it is waited upon. In such cases the method will throw the a {@link TachyonException} * with the appropriate {@link tachyon.exception.TachyonExceptionType} * <p/> * <i>IMPLEMENTATION NOTES</i> This method is implemented by periodically polling the master about * the file status. The polling period is controlled by the * {@link Constants#USER_FILE_WAITCOMPLETED_POLL_MS} java property and defaults to a generous 1 * second. * * @param fs an instance of {@link FileSystem} * @param uri the URI of the file whose completion status is to be watied for * @param timeout maximum time the calling thread should be blocked on this call * @param tunit the @{link TimeUnit} instance describing the {@code timeout} parameter * @return true if the file is complete when this method returns and false if the method timed out * before the file was complete. * @throws IOException in case there are problems contacting the Tachyonmaster for the file status * @throws TachyonException if a Tachyon Exception occurs * @throws InterruptedException if the thread receives an interrupt while waiting for file * completion */ public static boolean waitCompleted(final FileSystem fs, final TachyonURI uri, final long timeout, final TimeUnit tunit) throws IOException, TachyonException, InterruptedException { final long deadline = System.currentTimeMillis() + tunit.toMillis(timeout); final long pollPeriod = ClientContext.getConf().getLong(Constants.USER_FILE_WAITCOMPLETED_POLL_MS); boolean completed = false; long timeleft = deadline - System.currentTimeMillis(); long toSleep = 0; while (!completed && (timeout <= 0 || timeleft > 0)) { if (!fs.exists(uri)) { LOG.debug("The file {} being waited upon does not exist yet. Waiting for it to be " + "created.", uri); } else { completed = fs.getStatus(uri).isCompleted(); } if (timeout == 0) { return completed; } else if (!completed) { if (timeout < 0 || timeleft > pollPeriod) { toSleep = pollPeriod; } else { toSleep = timeleft; } CommonUtils.sleepMs(LOG, toSleep, true); timeleft = deadline - System.currentTimeMillis(); } } return completed; }
From source file:alluxio.client.file.FileSystemUtils.java
/** * Waits for a file to be marked as completed. * <p/>//from w w w. j a v a2s . c o m * The calling thread will block for <i>at most</i> {@code timeout} time units (as specified via * {@code tunit} or until the file is reported as complete by the master. The method will return * the last known completion status of the file (hence, false only if the method has timed out). A * zero value on the {@code timeout} parameter will make the calling thread check once and return; * a negative value will make it block indefinitely. Note that, in this last case, if a file is * never completed, the thread will block forever, so use with care. * <p/> * Note that the file whose uri is specified, might not exist at the moment this method this call. * The method will deliberately block anyway for the specified amount of time, waiting for the * file to be created and eventually completed. Note also that the file might be moved or deleted * while it is waited upon. In such cases the method will throw the a {@link AlluxioException} * <p/> * <i>IMPLEMENTATION NOTES</i> This method is implemented by periodically polling the master about * the file status. The polling period is controlled by the * {@link PropertyKey#USER_FILE_WAITCOMPLETED_POLL_MS} java property and defaults to a generous 1 * second. * * @param fs an instance of {@link FileSystem} * @param uri the URI of the file whose completion status is to be watied for * @param timeout maximum time the calling thread should be blocked on this call * @param tunit the @{link TimeUnit} instance describing the {@code timeout} parameter * @return true if the file is complete when this method returns and false if the method timed out * before the file was complete. * @throws IOException in case there are problems contacting the Alluxio Master * @throws AlluxioException if an Alluxio exception occurs * @throws InterruptedException if the thread receives an interrupt while waiting for file * completion */ public static boolean waitCompleted(final FileSystem fs, final AlluxioURI uri, final long timeout, final TimeUnit tunit) throws IOException, AlluxioException, InterruptedException { final long deadline = System.currentTimeMillis() + tunit.toMillis(timeout); final long pollPeriod = Configuration.getLong(PropertyKey.USER_FILE_WAITCOMPLETED_POLL_MS); boolean completed = false; long timeleft = deadline - System.currentTimeMillis(); while (!completed && (timeout <= 0 || timeleft > 0)) { if (!fs.exists(uri)) { LOG.debug("The file {} being waited upon does not exist yet. Waiting for it to be " + "created.", uri); } else { completed = fs.getStatus(uri).isCompleted(); } if (timeout == 0) { return completed; } else if (!completed) { long toSleep; if (timeout < 0 || timeleft > pollPeriod) { toSleep = pollPeriod; } else { toSleep = timeleft; } CommonUtils.sleepMs(LOG, toSleep); timeleft = deadline - System.currentTimeMillis(); } } return completed; }