Thread.join with retry pattern

Having looked at the sample code with help Thread, I often come across this replay pattern.

boolean retry = true;
while (retry) {
    try {
        myThread.join();
        retry = false;
    } catch (InterruptedException e) {
        // Nothing ...
    }
}

join() must wait forever.

If the current thread is interrupted earlier or during joinand thus receives InterruptedException, is it really myThreadjoining?

Is this some leftover cut paste in the pattern?

+4
source share
2 answers

The other is Threadnot join()yours Threadin the case InterruptedException. join()must wait always or until the call Threadis interrupt()-ed.

, InterruptedException. . :

  • . , , InterruptedException . ( ?)?
  • . , , , , Thread , InterruptedException.
  • . , , , , , .
  • . , Thread ( run()), InterruptedException.
  • . .
    • , Assertion, InterruptedException , , .
    • , , , InterruptedException.

log() InterruptedException - . , , InterruptedException.

, , . .

public static void joinThread(final Thread thread) {
    while (true)
        try {
            thread.join();
            return;
        } catch (final InterruptedException e) {
            Logger.getGlobal().log(Level.ERROR, "Unexpected InterruptedException", e);
        }
}

, .

public static void joinThread(final Thread thread) {
    try {
        thread.join();
    } catch (final InterruptedException e) {
        Logger.getGlobal().log(Level.ERROR, "Unexpected InterruptedException", e);
    }
}

interrupted.

, , Thread , , InterruptedException, .

public static void joinThread(final Thread thread) {
    try {
        thread.join();
    } catch (final InterruptedException e) {
        Logger.getGlobal().log(Level.INFO, "Unexpected InterruptedException, resetting flag", e);
        Thread.currentThread().interrupt();
    }
}

! , , . , ( Thread s), , interrupted . , , , , InterruptedException, , .

, , , .

public static void joinThread(final Thread thread) throws InterruptedException {
    try {
        thread.join();
    } catch (final InterruptedException e) {
        Logger.getGlobal().log(Level.FINE, "Got InterruptedException, propagating it to the caller", e);
        throw e;
    }
}

- , , .

.

, , - , , .

public static void joinThread(final Thread thread) {
    try {
        thread.join();
    } catch (final InterruptedException e) {
        Logger.getGlobal().log(Level.FINE, "Got InterruptedException, handling it", e);
        // ...whatever...
    }
}

.

, , Thread.interrupt() ThreadGroup.interrupt() , AssertionError . .

// Only throws AssertionError if assertions are enabled.
public static void joinThread(final Thread thread) {
    try {
        thread.join();
    } catch (final InterruptedException e) {
        Logger.getGlobal().log(Level.ERROR, "Unexpected InterruptedException", e);
        assert false : e;
    }
}

// Always throws AssertionError.
public static void joinThread(final Thread thread) {
    try {
        thread.join();
    } catch (final InterruptedException e) {
        Logger.getGlobal().log(Level.FATAL, "Unexpected InterruptedException", e);
        throw new AssertionError(e, "Unexpected Thread interruption.");
    }
}

InterruptedException , , , . , , , .

public static void joinThread(final Thread thread) {
    try {
        thread.join();
    } catch (final InterruptedException e) {
        Logger.getGlobal().log(Level.ERROR, "Unexpected InterruptedException", e);
        throw new SomeException(e, "Unexpected Thread interruption.");
    }
}

, . . 100 Thread.sleep() - - , . , . , Thread.interrupt() SIGHUP, SIGHUP .

, AssertionError. AssertionError - Throwable, Exception, , , - , AssertionError , , , , . , AssertionError , (-) , - , AssertionError UncaughtExceptionHandler Thread s.

+2

myThread.join() , . myThread , , , , .

, , . InterruptedException . . , InterruptedExceptions.

+1

All Articles