Java Code Examples for java.util.concurrent.TimeUnit.toMillis()

The following are Jave code examples for showing how to use toMillis() of the java.util.concurrent.TimeUnit class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: java-android-websocket-client   File: AbstractConnPool.java   Source Code and License Vote up 6 votes
/**
 * Closes connections that have been idle longer than the given period
 * of time and evicts them from the pool.
 *
 * @param idletime maximum idle time.
 * @param tunit time unit.
 */
public void closeIdle(final long idletime, final TimeUnit tunit) {
    Args.notNull(tunit, "Time unit");
    long time = tunit.toMillis(idletime);
    if (time < 0) {
        time = 0;
    }
    final long deadline = System.currentTimeMillis() - time;
    enumAvailable(new PoolEntryCallback<T, C>() {

        @Override
        public void process(final PoolEntry<T, C> entry) {
            if (entry.getUpdated() <= deadline) {
                entry.close();
            }
        }

    });
}
 
Example 2
Project: FirefoxData-android   File: BasicFuture.java   Source Code and License Vote up 6 votes
public synchronized T get(final long timeout, final TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException {
    Args.notNull(unit, "Time unit");
    final long msecs = unit.toMillis(timeout);
    final long startTime = (msecs <= 0) ? 0 : System.currentTimeMillis();
    long waitTime = msecs;
    if (this.completed) {
        return getResult();
    } else if (waitTime <= 0) {
        throw new TimeoutException();
    } else {
        for (;;) {
            wait(waitTime);
            if (this.completed) {
                return getResult();
            } else {
                waitTime = msecs - (System.currentTimeMillis() - startTime);
                if (waitTime <= 0) {
                    throw new TimeoutException();
                }
            }
        }
    }
}
 
Example 3
Project: util4j   File: FixedThreadPoolQueuesExecutor.java   Source Code and License Vote up 6 votes
/**
 * {@inheritDoc}
 */
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {

    long deadline = System.currentTimeMillis() + unit.toMillis(timeout);

    synchronized (workers) {
        while (!isTerminated()) {
            long waitTime = deadline - System.currentTimeMillis();
            if (waitTime <= 0) {
                break;
            }

            workers.wait(waitTime);
        }
    }
    return isTerminated();
}
 
Example 4
Project: neoscada   File: Request.java   Source Code and License Vote up 6 votes
public Request(Object id, Object message, boolean useResponseQueue, long timeout, TimeUnit unit) {
    if (id == null) {
        throw new IllegalArgumentException("id");
    }
    if (message == null) {
        throw new IllegalArgumentException("message");
    }
    if (timeout < 0) {
        throw new IllegalArgumentException("timeout: " + timeout + " (expected: 0+)");
    } else if (timeout == 0) {
        timeout = Long.MAX_VALUE;
    }

    if (unit == null) {
        throw new IllegalArgumentException("unit");
    }

    this.id = id;
    this.message = message;
    this.responses = useResponseQueue ? new LinkedBlockingQueue<Object>() : null;
    this.timeoutMillis = unit.toMillis(timeout);
}
 
Example 5
Project: JRediClients   File: RedissonExecutorService.java   Source Code and License Vote up 5 votes
@Override
public RScheduledFuture<?> scheduleWithFixedDelayAsync(Runnable task, long initialDelay, long delay, TimeUnit unit) {
    check(task);
    byte[] classBody = getClassBody(task);
    byte[] state = encode(task);
    long startTime = System.currentTimeMillis() + unit.toMillis(initialDelay);
    RemotePromise<Void> result = (RemotePromise<Void>) asyncScheduledServiceAtFixed.scheduleWithFixedDelay(task.getClass().getName(), classBody, state, startTime, unit.toMillis(delay));
    addListener(result);
    return new RedissonScheduledFuture<Void>(result, startTime);
}
 
Example 6
Project: Reactive4JavaFlow   File: TrampolineSchedulerService.java   Source Code and License Vote up 5 votes
@Override
public AutoDisposable schedule(Runnable task, long delay, TimeUnit unit) {
    if (!closed) {
        TrampolineTask tt = new TrampolineTask(task, unit.toMillis(delay));
        queue.offer(tt);
        drain();
        return tt;
    }
    return REJECTED;
}
 
Example 7
Project: smart-id-java-client   File: SmartIdRestConnector.java   Source Code and License Vote up 5 votes
private void addResponseSocketOpenTimeUrlParameter(SessionStatusRequest request, UriBuilder uriBuilder) {
  if (request.isResponseSocketOpenTimeSet()) {
    TimeUnit timeUnit = request.getResponseSocketOpenTimeUnit();
    long timeValue = request.getResponseSocketOpenTimeValue();
    long queryTimeoutInMilliseconds = timeUnit.toMillis(timeValue);
    uriBuilder.queryParam("timeoutMs", queryTimeoutInMilliseconds);
  }
}
 
Example 8
Project: oksse   File: RealServerSentEvent.java   Source Code and License Vote up 5 votes
@Override
public void setTimeout(long timeout, TimeUnit unit) {
    if (sseReader != null) {
        sseReader.setTimeout(timeout, unit);
    }
    readTimeoutMillis = unit.toMillis(timeout);
}
 
Example 9
Project: downloader   File: Config.java   Source Code and License Vote up 5 votes
private long checkTimeoutArgument(long timeout, TimeUnit unit) {
    if (timeout < 0) throw new IllegalArgumentException("timeout < 0");
    if (unit == null) throw new NullPointerException("unit == null");
    long millis = unit.toMillis(timeout);
    if (millis > Integer.MAX_VALUE)
        throw new IllegalArgumentException("Timeout too large.");
    if (millis == 0 && timeout > 0)
        throw new IllegalArgumentException("Timeout too small.");
    return millis;
}
 
Example 10
Project: log4j2-extended-jsonlayout   File: ListAppender.java   Source Code and License Vote up 5 votes
/**
 * Polls the messages list for it to grow to a given minimum size at most timeout timeUnits and return a copy of
 * what we have so far.
 */
public List<String> getMessages(final int minSize, final long timeout, final TimeUnit timeUnit) throws InterruptedException {
    final long endMillis = System.currentTimeMillis() + timeUnit.toMillis(timeout);
    while (messages.size() < minSize && System.currentTimeMillis() < endMillis) {
        Thread.sleep(100);
    }
    return Collections.unmodifiableList(messages);
}
 
Example 11
Project: datarouter   File: Cached.java   Source Code and License Vote up 4 votes
public Cached(long cacheFor, TimeUnit timeUnit){
	this.cacheForMs = timeUnit.toMillis(cacheFor);
}
 
Example 12
Project: outland   File: OkHttpResource.java   Source Code and License Vote up 4 votes
public OkHttpResource readTimeout(long timeout, TimeUnit unit) {
  this.readTimeout = unit.toMillis(timeout);
  hasPerRequestReadTimeout = true;
  return this;
}
 
Example 13
Project: boohee_v5.6   File: TrampolineScheduler.java   Source Code and License Vote up 4 votes
public Subscription schedule(Action0 action, long delayTime, TimeUnit unit) {
    long execTime = now() + unit.toMillis(delayTime);
    return enqueue(new SleepingAction(action, this, execTime), execTime);
}
 
Example 14
Project: Equella   File: BatchedCache.java   Source Code and License Vote up 4 votes
/**
 * Automatically flush when putting or invalidating if it's been more than
 * the given time period.
 */
public synchronized void setAutoFlush(long duration, TimeUnit unit)
{
	autoFlushDuration = unit.toMillis(duration);
}
 
Example 15
Project: boohee_v5.6   File: ReplaySubject.java   Source Code and License Vote up 4 votes
public static <T> ReplaySubject<T> createWithTimeAndSize(long time, TimeUnit unit, int size, Scheduler scheduler) {
    BoundedState<T> state = new BoundedState(new PairEvictionPolicy(new SizeEvictionPolicy(size), new TimeEvictionPolicy(unit.toMillis(time), scheduler)), new AddTimestamped(scheduler), new RemoveTimestamped());
    return createWithState(state, new TimedOnAdd(state, scheduler));
}
 
Example 16
Project: JRediClients   File: RedissonPermitExpirableSemaphore.java   Source Code and License Vote up 4 votes
private long calcTimeout(long ttl, TimeUnit timeUnit) {
    if (ttl != -1) {
        return System.currentTimeMillis() + timeUnit.toMillis(ttl);
    }
    return nonExpirableTimeout;
}
 
Example 17
Project: moviediary   File: CachingService.java   Source Code and License Vote up 4 votes
public CacheItem<T> setTimeout(TimeUnit unit, long length) {
    timeout = unit.toMillis(length);
    return this;
}
 
Example 18
Project: xtf   File: Timer.java   Source Code and License Vote up 4 votes
public Timer(final long till, final TimeUnit timeUnit) {
	this.interval = timeUnit.toMillis(till);
	reset();
}
 
Example 19
Project: vind   File: Facet.java   Source Code and License Vote up 3 votes
/**
 * Constructor of the {@link DateMathRangeFacet} providing all the needed configuration.
 * @param name String with a custom name for the instance of the facet. It should be alphanumeric.
 * @param fieldDescriptor {@link FieldDescriptor} to do the range query on. T must extend {@link Date}.
 * @param start T Starting value of the defined range. T must extend {@link DateMathExpression}.
 * @param end T Ending value of the defined range. T must extend {@link DateMathExpression}.
 * @param gap Long Size of the steps in which the range is divided to facet the results.
 * @param timeUnit TimeUnit Units in which the gap is given.
 * @param pivotNames Name of the pivots using this facet.
 */
public DateMathRangeFacet(String name, FieldDescriptor<? extends Date> fieldDescriptor, T start, T end, Long gap,TimeUnit timeUnit, String ... pivotNames) {
    this.name = name;
    this.fieldDescriptor = fieldDescriptor;
    this.start = start;
    this.end = end;
    this.gap = timeUnit.toMillis(gap);
    this.tagedPivots = pivotNames;
}
 
Example 20
Project: Jaffree   File: UrlInOut.java   Source Code and License Vote up 2 votes
/**
 * Like the {@link #setPositionEof(long)}  (-ss) option but relative to the "end of file".
 * That is negative values are earlier in the file, 0 is at EOF.
 *
 * @param positionEof position, relative to the EOF
 * @param unit        time unit
 * @return this
 * @see #setPositionEof(long)
 */
public T setPositionEof(long positionEof, TimeUnit unit) {
    this.positionEof = unit.toMillis(positionEof);
    return thisAsT();
}