Java Sleep sleepUntil(long time)

Here you can find the source of sleepUntil(long time)

Description

Sleep until a particular time.

License

Open Source License

Parameter

Parameter Description
time the long time in milliseconds to sleep until

Return

true if delay succeeded, false if interrupted 100 times

Declaration

public static boolean sleepUntil(long time) 

Method Source Code

//package com.java2s;
/* *******************************************************************
 * Copyright (c) 1999-2001 Xerox Corporation, 
 *               2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved. /* w  w w .  jav  a 2s .  c o  m*/
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *     Xerox/PARC     initial implementation 
 * ******************************************************************/

public class Main {
    /**
     * Sleep until a particular time.
     * 
     * @param time the long time in milliseconds to sleep until
     * @return true if delay succeeded, false if interrupted 100 times
     */
    public static boolean sleepUntil(long time) {
        if (time == 0) {
            return true;
        } else if (time < 0) {
            throw new IllegalArgumentException("negative: " + time);
        }
        // final Thread thread = Thread.currentThread();
        long curTime = System.currentTimeMillis();
        for (int i = 0; (i < 100) && (curTime < time); i++) {
            try {
                Thread.sleep(time - curTime);
            } catch (InterruptedException e) {
                // ignore
            }
            curTime = System.currentTimeMillis();
        }
        return (curTime >= time);
    }
}

Related

  1. sleepThread(long millSecd)
  2. sleepThread(long sleeptime)
  3. sleepTight(final long millis)
  4. sleepUninterruptedly(long millis)
  5. sleepUninterruptibly(long msecs)
  6. sleepUntil(long timestamp)
  7. sleepUpTo(final long millis)
  8. sleepWithoutInterrupt(final long msToWait)
  9. sleepWithoutInterruptions(long milliseconds)