/** * Invokes {@code unit.}{@link TimeUnit#timedJoin(Thread, long) timedJoin(toJoin, timeout)} * uninterruptibly. */ @GwtIncompatible // concurrency @SuppressWarnings("GoodTime") // should accept a java.time.Duration public static void joinUninterruptibly(Thread toJoin, long timeout, TimeUnit unit) { Preconditions.checkNotNull(toJoin); boolean interrupted = false; try { long remainingNanos = unit.toNanos(timeout); long end = System.nanoTime() + remainingNanos; while (true) { try { // TimeUnit.timedJoin() treats negative timeouts just like zero. NANOSECONDS.timedJoin(toJoin, remainingNanos); return; } catch (InterruptedException e) { interrupted = true; remainingNanos = end - System.nanoTime(); } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } }
/** * Invokes {@code unit.}{@link TimeUnit#timedJoin(Thread, long) timedJoin(toJoin, timeout)} * uninterruptibly. */ @GwtIncompatible // concurrency public static void joinUninterruptibly(Thread toJoin, long timeout, TimeUnit unit) { Preconditions.checkNotNull(toJoin); boolean interrupted = false; try { long remainingNanos = unit.toNanos(timeout); long end = System.nanoTime() + remainingNanos; while (true) { try { // TimeUnit.timedJoin() treats negative timeouts just like zero. NANOSECONDS.timedJoin(toJoin, remainingNanos); return; } catch (InterruptedException e) { interrupted = true; remainingNanos = end - System.nanoTime(); } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } }
/** * Invokes {@code unit.}{@link TimeUnit#timedJoin(Thread, long) timedJoin(toJoin, timeout)} * uninterruptibly. */ @GwtIncompatible // concurrency public static void joinUninterruptibly(Thread toJoin, long timeout, TimeUnit unit) { Preconditions.checkNotNull(toJoin); boolean interrupted = false; try { long remainingNanos = unit.toNanos(timeout); long end = System.nanoTime() + remainingNanos; while (true) { try { // TimeUnit.timedJoin() treats negative timeouts just like zero. NANOSECONDS.timedJoin(toJoin, remainingNanos); return; } catch (InterruptedException e) { interrupted = true; remainingNanos = end - System.nanoTime(); } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } }
/** * Invokes * {@code unit.}{@link TimeUnit#timedJoin(Thread, long) * timedJoin(toJoin, timeout)} uninterruptibly. */ public static void joinUninterruptibly(Thread toJoin, long timeout, TimeUnit unit) { Preconditions.checkNotNull(toJoin); boolean interrupted = false; try { long remainingNanos = unit.toNanos(timeout); long end = System.nanoTime() + remainingNanos; while (true) { try { // TimeUnit.timedJoin() treats negative timeouts just like zero. NANOSECONDS.timedJoin(toJoin, remainingNanos); return; } catch (InterruptedException e) { interrupted = true; remainingNanos = end - System.nanoTime(); } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } }
private static void joinUninterruptibly(Thread thread, long timeout, TimeUnit unit) { boolean interrupted = false; try { long remainingNanos = unit.toNanos(timeout); long end = System.nanoTime() + remainingNanos; while (true) { try { // TimeUnit.timedJoin() treats negative timeouts just like zero. NANOSECONDS.timedJoin(thread, remainingNanos); return; } catch (InterruptedException e) { interrupted = true; remainingNanos = end - System.nanoTime(); } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } } }
/** * Invokes {@code unit.}{@link TimeUnit#timedJoin(Thread, long) timedJoin(toJoin, timeout)} * uninterruptibly. */ @GwtIncompatible // concurrency public static void joinUninterruptibly(Thread toJoin, long timeout, TimeUnit unit) { Preconditions.checkNotNull(toJoin); boolean interrupted = false; try { long remainingNanos = unit.toNanos(timeout); long end = System.nanoTime() + remainingNanos; while (true) { try { // TimeUnit.timedJoin() treats negative timeouts just like zero. NANOSECONDS.timedJoin(toJoin, remainingNanos); return; } catch (InterruptedException e) { interrupted = true; remainingNanos = end - System.nanoTime(); } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } }
unit.timedJoin(targetThread, interval); if (!isDone && targetThread.isAlive()) { System.out.format("%s test thread did not respond to Thread.interrupt; forcefully stopping %s%n",
@Override public void timedJoin(Thread thread) throws InterruptedException { unit.timedJoin(thread, time); }
/** * Waits until a process finishes or a timeout occurs * * @param p process * @param timeout timeout in given unit * @param unit time unit * @return the process exit value * @throws TimeoutException if a timeout occurs */ public static int waitForWithTimeout(final Process p, long timeout, TimeUnit unit) throws TimeoutException { ProcessThread t = new ProcessThread(p); t.start(); try { unit.timedJoin(t, timeout); } catch (InterruptedException e) { t.interrupt(); Thread.currentThread().interrupt(); } if (!t.hasFinished()) { throw new TimeoutException("Process did not finish within timeout"); } return t.exitValue(); } }
@Override public void timedJoin(Thread thread) throws InterruptedException { unit.timedJoin(thread, time); }
public void timedJoin(final Thread thread) throws InterruptedException { timeUnit.timedJoin(thread, duration); }
public void timedJoin(final Thread thread) throws InterruptedException { timeUnit.timedJoin(thread, duration); }
public void join(final Thread thread) throws InterruptedException { checkNotNull(thread); unit.timedJoin(thread, value); }
public static void main(String[] args) throws InterruptedException { //睡眠13分钟 TimeUnit.MINUTES.sleep(13); //Thread.sleep(780000);//这样写你知道是多久吗? //Thread.sleep(13*60*1000);//这样写会稍微好些 //睡眠1小时 TimeUnit.HOURS.sleep(1); //Thread.sleep(3600000); TimeUnitTest test = new TimeUnitTest(); Thread thread = new Thread(() -> test.work()); //10秒内Join TimeUnit.SECONDS.timedJoin(thread,10); //thread.join(10000); }
public void stop(long timeout, TimeUnit unit) throws Exception { // if(!this.running.compareAndSet(true, false)) { return; } // try { // final long now = System.nanoTime(); doStop(timeout, unit); timeout -= unit.convert(System.nanoTime() - now, TimeUnit.NANOSECONDS); // if(timeout > 0) { unit.timedJoin(this.worker, timeout); this.worker = null; } } finally { notifyOnStop(); } }
public void stop(long timeout, TimeUnit unit) throws Exception { // if(!this.running.compareAndSet(true, false)) { return; } // try { // final long now = System.nanoTime(); doStop(timeout, unit); timeout -= unit.convert(System.nanoTime() - now, TimeUnit.NANOSECONDS); // if(timeout > 0) { unit.timedJoin(this.worker, timeout); this.worker = null; } } finally { notifyOnStop(); } }
/** * Invokes {@code unit.}{@link TimeUnit#timedJoin(Thread, long) timedJoin(toJoin, timeout)} * uninterruptibly. */ @GwtIncompatible // concurrency public static void joinUninterruptibly(Thread toJoin, long timeout, TimeUnit unit) { Preconditions.checkNotNull(toJoin); boolean interrupted = false; try { long remainingNanos = unit.toNanos(timeout); long end = System.nanoTime() + remainingNanos; while (true) { try { // TimeUnit.timedJoin() treats negative timeouts just like zero. NANOSECONDS.timedJoin(toJoin, remainingNanos); return; } catch (InterruptedException e) { interrupted = true; remainingNanos = end - System.nanoTime(); } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } }
/** * Invokes {@code unit.}{@link TimeUnit#timedJoin(Thread, long) timedJoin(toJoin, timeout)} * uninterruptibly. */ @GwtIncompatible // concurrency public static void joinUninterruptibly(Thread toJoin, long timeout, TimeUnit unit) { Preconditions.checkNotNull(toJoin); boolean interrupted = false; try { long remainingNanos = unit.toNanos(timeout); long end = System.nanoTime() + remainingNanos; while (true) { try { // TimeUnit.timedJoin() treats negative timeouts just like zero. NANOSECONDS.timedJoin(toJoin, remainingNanos); return; } catch (InterruptedException e) { interrupted = true; remainingNanos = end - System.nanoTime(); } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } }
/** * Invokes {@code unit.}{@link TimeUnit#timedJoin(Thread, long) timedJoin(toJoin, timeout)} * uninterruptibly. */ @GwtIncompatible // concurrency public static void joinUninterruptibly(Thread toJoin, long timeout, TimeUnit unit) { Preconditions.checkNotNull(toJoin); boolean interrupted = false; try { long remainingNanos = unit.toNanos(timeout); long end = System.nanoTime() + remainingNanos; while (true) { try { // TimeUnit.timedJoin() treats negative timeouts just like zero. NANOSECONDS.timedJoin(toJoin, remainingNanos); return; } catch (InterruptedException e) { interrupted = true; remainingNanos = end - System.nanoTime(); } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } }
private static void joinUninterruptibly( Thread thread, long timeout, TimeUnit unit) { boolean interrupted = false; try { long remainingNanos = unit.toNanos(timeout); long end = System.nanoTime() + remainingNanos; while (true) { try { // TimeUnit.timedJoin() treats negative timeouts just like zero. NANOSECONDS.timedJoin(thread, remainingNanos); return; } catch (InterruptedException e) { interrupted = true; remainingNanos = end - System.nanoTime(); } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } } }