How to capture a scope in Runnable

I am trying to do an update stream on HandlerThreadusing the following class, but I have a few questions about how variable capture in Java works.

[1] Is retcaptured from the scope by reference?

[2] Whether Indicates thison Runnable, or it is removed from the field of action?

[bonus] StartStream should send a message Runnableto the handler thread and return only after completion Runnable. Will the code below work properly?

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;
    }
}
+2
source share
2 answers

Inner classes have implicit references to an outer class.

ret , . , , , . java version. " " -.

this Runnable, Stream.this .

+3

ret - , , . , , ret . , [1] .

, Java , (, ) , , .

:

this Runnable. Stream.this Stream.

+1

All Articles