2

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?

Keselme
  • 3,779
  • 7
  • 36
  • 68
  • 1
    Yes, the call is executed on the thread that executes the `Task` runnable. Are you trying to hand over some work from `Task` to be executed on the main thread? – Janus Varmarken Jul 30 '18 at 19:11
  • 1
    Sorry, but how did you make `new Task(this)) ` in `static main` compile? Perhaps, you meant `new Task(new MyMain())` – Andrew Tobilko Jul 30 '18 at 19:15
  • @JanusVarmarken no, I want to notify the parent thread that the child thread is done. I know I can use Thread.join() in the parent thread, I just thought that there can also be another way of notification. – Keselme Jul 30 '18 at 19:18
  • @AndrewTobilko i didn't actually compile it, I wrote the example code here in the question editor. – Keselme Jul 30 '18 at 19:18
  • @Keselme, you are an accurate writer then, but next time, please, make sure it compiles – Andrew Tobilko Jul 30 '18 at 19:19
  • there is no such a thing "child thread" in Java (unlike process). and the observable pattern is for things to be done not about who done it unless you use a better technique as at platform.runlater in JavaFX – Sarel Foyerlicht Jul 30 '18 at 19:21
  • @JanusVarmarken, what if I did want to hand over some work from Task to the main thread, would it be the correct way to do it? – Keselme Jul 30 '18 at 19:33
  • When there is some piece of work to be done, the correctness of your program should not depend on which thread does the work. (There might be some _performance_ reason why it matters, but that's a more advanced topic.) – Solomon Slow Jul 30 '18 at 21:17
  • @Keselme: is this for plain Java or Android? – Janus Varmarken Jul 30 '18 at 22:41
  • @JanusVarmarken it's for plain Java. In android i guess I can use AsyncTask for short time jobs. – Keselme Jul 31 '18 at 06:35

1 Answers1

1

Yes, you will be on the same thread that did the work.

If you don't want to be running in that thread (if you want your original thread to "Resume" after the work is done) then use Thread.join instead.

It's like this (Please excuse horrible on the fly ascii art):

mainThread -- start worker thread -- do parallel work -- join -- Continue
                  \                                       /
             Worker thread started--do parallel work--exit thread

At the end ("continue") you will be guaranteed that the worker thread has finished completely.

Bill K
  • 62,186
  • 18
  • 105
  • 157
  • join blocks the main thread from further execution, right? But what if I want the main thread still run and be responsive and still get notified when worker thread is done? – Keselme Jul 30 '18 at 19:28
  • 1
    @Keselme I think you might be interested to check out classes like EventQueue or Android's Looper. Also I think this answer https://stackoverflow.com/questions/7597742/what-is-the-purpose-of-looper-and-how-to-use-it/34522758#34522758 can be useful for you – Denis Zavedeev Jul 30 '18 at 19:38