forked from apache/commons-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadUtils.java.html
54 lines (49 loc) · 3.49 KB
/
ThreadUtils.java.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="it"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>ThreadUtils.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Commons IO</a> > <a href="index.source.html" class="el_package">org.apache.commons.io</a> > <span class="el_source">ThreadUtils.java</span></div><h1>ThreadUtils.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.commons.io;
import java.time.Duration;
import java.time.Instant;
/**
* Helps work with threads.
*
* @since 2.12.0
*/
<span class="nc" id="L27">public class ThreadUtils {</span>
static int getNanosOfMilli(final Duration duration) {
<span class="fc" id="L30"> return duration.getNano() % 1_000_000;</span>
}
/**
* Sleeps for a guaranteed minimum duration unless interrupted.
*
* This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else it deems appropriate.
* Read {@link Thread#sleep(long, int)}} for further interesting details.
*
* TODO The above needs confirmation now that we've been on Java 8 for a while.
*
* @param duration the sleep duration.
* @throws InterruptedException if interrupted.
*/
public static void sleep(final Duration duration) throws InterruptedException {
<span class="fc" id="L45"> final Instant finishInstant = Instant.now().plus(duration);</span>
<span class="fc" id="L46"> Duration remainingDuration = duration;</span>
do {
<span class="fc" id="L48"> Thread.sleep(remainingDuration.toMillis(), getNanosOfMilli(remainingDuration));</span>
<span class="fc" id="L49"> remainingDuration = Duration.between(Instant.now(), finishInstant);</span>
<span class="pc bpc" id="L50" title="1 of 2 branches missed."> } while (!remainingDuration.isNegative());</span>
<span class="fc" id="L51"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.8.202204050719</span></div></body></html>