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() {
return 1;
}
public int StartStream() {
int ret = -1;
handler.post(new Runnable(){
@Override public void run() {
synchronized(this) {
ret = _startStream();
this.notify();
}
}
});
synchronized(this) {
while(ret == -1) {
try {
this.wait();
}
catch (InterruptedException e){}
}
}
return ret;
}
}
source
share