@Test public void testPostAndReply() { final int destructAfterLastActivityInMs = 300; final Integer expectedResult = 123; final Callable<Integer> callable = new Callable<Integer>() { @Override public Integer call() throws Exception { return expectedResult; } }; final WaitableReplyCallback reply = new WaitableReplyCallback(); final SelfDestructiveThread thread = new SelfDestructiveThread( "test", Process.THREAD_PRIORITY_BACKGROUND, destructAfterLastActivityInMs); InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { @Override public void run() { thread.postAndReply(callable, reply); } }); assertEquals(expectedResult, reply.waitUntil(DEFAULT_TIMEOUT)); }
public <T> void postAndReply(final Callable<T> callable, final ReplyCallback<T> reply) { final Handler callingHandler = new Handler(); post(new Runnable() { @Override public void run() { T t; try { t = callable.call(); } catch (Exception e) { t = null; } final T result = t; callingHandler.post(new Runnable() { @Override public void run() { reply.onReply(result); } }); } }); }
SelfDestructiveThread { public <T> void postAndReply(final Callable<T> callable, final ReplyCallback<T> reply) { final Handler callingHandler = new Handler(); post(new Runnable() { @Override public void run() { T t; try { t = callable.call(); } catch (Exception e) { t = null; } final T result = t; callingHandler.post(new Runnable() { @Override public void run() { reply.onReply(result); } }); } }); } }
SelfDestructiveThread { public <T> void postAndReply(final Callable<T> callable, final ReplyCallback<T> reply) { final Handler callingHandler = new Handler(); post(new Runnable() { @Override public void run() { T t; try { t = callable.call(); } catch (Exception e) { t = null; } final T result = t; callingHandler.post(new Runnable() { @Override public void run() { reply.onReply(result); } }); } }); } SelfDestructiveThread(
            String threadName, int priority, int destructAfterMillisec); }
SelfDestructiveThread { public <T> void postAndReply(final Callable<T> callable, final ReplyCallback<T> reply) { final Handler callingHandler = new Handler(); post(new Runnable() { @Override public void run() { T t; try { t = callable.call(); } catch (Exception e) { t = null; } final T result = t; callingHandler.post(new Runnable() { @Override public void run() { reply.onReply(result); } }); } }); } SelfDestructiveThread(
            String threadName, int priority, int destructAfterMillisec); @VisibleForTesting boolean isRunning(); @VisibleForTesting int getGeneration(); void postAndReply(final Callable<T> callable, final ReplyCallback<T> reply); T postAndWait(final Callable<T> callable, int timeoutMillis); }
SelfDestructiveThread { public <T> void postAndReply(final Callable<T> callable, final ReplyCallback<T> reply) { final Handler callingHandler = new Handler(); post(new Runnable() { @Override public void run() { T t; try { t = callable.call(); } catch (Exception e) { t = null; } final T result = t; callingHandler.post(new Runnable() { @Override public void run() { reply.onReply(result); } }); } }); } SelfDestructiveThread(
            String threadName, int priority, int destructAfterMillisec); @VisibleForTesting boolean isRunning(); @VisibleForTesting int getGeneration(); void postAndReply(final Callable<T> callable, final ReplyCallback<T> reply); T postAndWait(final Callable<T> callable, int timeoutMillis);  }