How to capture enclosing scope in Runnable
<p>I'm trying to do a stream of updates on a <code>HandlerThread</code> using the class below, but I have a couple of questions about how variable capture works in Java.</p>

<p><strong>[1]</strong> Is <code>ret</code> captured from the enclosing scope by reference?</p>

<p><strong>[2]</strong> Does <code>this</code> refer to the <code>Runnable</code>, or is it captured from the enclosing scope?</p>

<p><strong>[bonus]</strong> <code>StartStream</code> should post a <code>Runnable</code> to the handler thread, and only return when the <code>Runnable</code> has completed. Will the code below work as expected?</p>

<pre><code>public class Stream extends HandlerThread {
    Handler handler = null;

    Stream() {
        super("Stream");
        handler = new Handler(getLooper());
        start();
    }

    private int _startStream() { // Start some repeating update
        return 1;
    }

    public int StartStream() {
        int ret = -1;

        handler.post(new Runnable(){
            @Override public void run() {
                synchronized(this) {
                    ret = _startStream();    // [1]
                    this.notify();           // [2]
                }
            }
        });

        synchronized(this) {
            while(ret == -1) {
                try {
                    this.wait();
                }
                catch (InterruptedException e){}
            }
        }

        return ret;
    }
}
</code></pre>