diff --git a/core/java/android/os/HandlerThread.java b/core/java/android/os/HandlerThread.java index 92fcbb65b1064..4dd797a70fcb3 100644 --- a/core/java/android/os/HandlerThread.java +++ b/core/java/android/os/HandlerThread.java @@ -71,23 +71,35 @@ public class HandlerThread extends Thread { /** * This method returns the Looper associated with this thread. If this thread not been started * or for any reason isAlive() returns false, this method will return null. If this thread - * has been started, this method will block until the looper has been initialized. + * has been started, this method will block until the looper has been initialized. * @return The looper. */ public Looper getLooper() { if (!isAlive()) { return null; } - + + boolean wasInterrupted = false; + // If the thread has been started, wait until the looper has been created. synchronized (this) { while (isAlive() && mLooper == null) { try { wait(); } catch (InterruptedException e) { + wasInterrupted = true; } } } + + /* + * We may need to restore the thread's interrupted flag, because it may + * have been cleared above since we eat InterruptedExceptions + */ + if (wasInterrupted) { + Thread.currentThread().interrupt(); + } + return mLooper; }