there is something that bothers me a little about thread notification when using observer-observable pattern. What I mean is, can I notify my main thread that the worker thread is done by using observer-observable pattern?
Let's say we have this interface:
interface MyInterface{
void onThreadFinished(Object result);
}
Let's say we have the following class that implements Runnable:
class Task implements Runnable{
MyInterface listener;
Task(MyInterface listener){
this.listener = listener;
}
public void run(){
Object result = new Object();
try{
//do some work on result here
}
finally{
listener.onThreadFinished(result);
}
}
}
And now out main thread:
class MyMain implements MyInterface{
public void onThreadFinished(Object result){
//Do something with the result
}
public static void main(String args[]){
Task myTask = new Task(this);
Thread thread = new Thread(myTask);
thread.start();
}
}
Now, my question is, when I call listener.onThreadFinished(result); from the finally block, I'm still in run() method meaning that the worker thread isn't finished yet, so everything that happens in the implementation of the interface in MyMain class still runs on the thread. Am I correct here or missing something?