Does the observer template use as a notifier in the same way as with Thread.join?

Uses a connection to wait for a thread to terminate just like using an observer to notify the class of the calling thread when the thread is finished?

Using join - 

public class Reader {

    Thread t = new Thread(new ReadContent());
    t.start();
    t.join();
    System.out.println("Thread has finished");

        public class ReadContent() implements Runnable{
        }

        public void run() {
            readContentURL(); 
        }
}

/*****************************************************************************/
    Using observer - 


public interface ReadContentListener{

    public void contentRead();

}


 public class Reader implements ReadContentListener {

    Thread t = new Thread(new ReadContent(this));
    t.start();

    pubic void contentRead(){
     System.out.println("Thread has finished");
    }

        public class ReadContent implements Runnable{

        public ReadContent(ReadContentListener readContentListener) {
            this.readContentListener = readContentListener;
        }

        public void run() {
            readContentURL(); 
            this.readContentListener.contentRead();
        }
}


/*************************************************************/

    public GenericScreenAnimation(String nextScreen, Thread genericWorkerThread)
{

         this.genericWorkerThread = genericWorkerThread;
         this.genericWorkerThread.start();
         initialise();
         try {
            this.genericWorkerThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ScreenController.displayNextScreen(nextScreen);
}
+5
source share
3 answers

No, they are not the same. I would say that using join () in this context is not good programming practice - why use Threads otherwise? You essentially turn an asynchronous operation into a synchronous one.

Observer , UI, , , GUI (, , SwingWorker - , ).

+1

, . ReadContentListener , . ( , ).

Thread.join, , . .start(), , , . .start() ( ) .

+4

, , , :

  • join , ,
  • join ( ) , , .
+1

All Articles