public int execute(final InterruptibleProcess worker, final long timeout) throws IOException, ExecutionException, TimeoutException, InterruptedException { myErrStreamListener = createStreamListener(worker.getErrorStream()); myInStreamListener = createStreamListener(worker.getInputStream()); final Application app = ApplicationManager.getApplication(); Future<?> errorStreamReadingFuture = null; Future<?> outputStreamReadingFuture = null; final int rc; try { errorStreamReadingFuture = app.executeOnPooledThread(myErrStreamListener); outputStreamReadingFuture = app.executeOnPooledThread(myInStreamListener); rc = worker.execute(); if (tryReadStreams(rc)) { errorStreamReadingFuture.get(timeout, TimeUnit.MILLISECONDS); outputStreamReadingFuture.get(timeout, TimeUnit.MILLISECONDS); } } finally { cancelListeners(); if (errorStreamReadingFuture != null) { errorStreamReadingFuture.cancel(true); } if (outputStreamReadingFuture != null) { outputStreamReadingFuture.cancel(true); } } return rc; }