class SelfDestructiveThread {

    public <T> T postAndWait(final Callable<T> callable, int timeoutMillis)
            throws InterruptedException {
        final ReentrantLock lock = new ReentrantLock();
        final Condition cond = lock.newCondition();

        final AtomicReference<T> holder = new AtomicReference();
        final AtomicBoolean running = new AtomicBoolean(true);
        post(new Runnable() {
            @Override
            public void run() {
                try {
                    holder.set(callable.call());
                } catch (Exception e) {
                    // Do nothing.
                }
                lock.lock();
                try {
                    running.set(false);
                    cond.signal();
                } finally {
                    lock.unlock();
                }
            }
        });

        lock.lock();
        try {
            if (!running.get()) {
                return holder.get();  // already finished.
            }
            long remaining = TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
            for (;;) {
                try {
                    remaining = cond.awaitNanos(remaining);
                } catch (InterruptedException e) {
                    // ignore
                }
                if (!running.get()) {
                    return holder.get();  // Successfully finished.
                }
                if (remaining <= 0) {
                    throw new InterruptedException("timeout");  // Timeout
                }
            }
        } finally {
            lock.unlock();
        }
    }

    public  SelfDestructiveThread(
            String threadName, int priority, int destructAfterMillisec);

}