Java Sleep sleepMSInChunks(long ms)

Here you can find the source of sleepMSInChunks(long ms)

Description

Sleeps in chunks of 10ms, to prevent the risk of a GC eating the whole sleeping duration, and not letting program enough duration to make progress.

License

Apache License

Parameter

Parameter Description
ms Duration to sleep for, in milliseconds.

Declaration

public static void sleepMSInChunks(long ms) 

Method Source Code

//package com.java2s;
/*/*  ww  w  .  j  av a 2s.  co  m*/
 * Copyright 2012 Jeff Hain
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Sleeps in chunks of 10ms, to prevent the risk of a GC
     * eating the whole sleeping duration, and not letting
     * program enough duration to make progress.
     * 
     * Useful to ensure GC-proof-ness of tests sleeping for
     * some time to let concurrent treatment make progress.
     * 
     * @param ms Duration to sleep for, in milliseconds.
     */
    public static void sleepMSInChunks(long ms) {
        final long chunkMS = 10;
        while (ms >= chunkMS) {
            try {
                Thread.sleep(chunkMS);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            ms -= chunkMS;
        }
    }
}

Related

  1. sleepMillis(long millis)
  2. sleepMillis(long millis)
  3. sleepMilliseconds(long ms)
  4. sleepMilliseconds(long ms)
  5. sleepMs(final int millis)
  6. sleepNanos(final long nanoDuration)
  7. sleepNanos(long nanoseconds)
  8. sleepNoException(int ms)
  9. sleepNoShit(long nanos)