开发者

Is using observer pattern as a notifier same as using Thread.join?

Is using a join to wait until a Thread has finished the same as using the an observer to notify the calling thread class when the thread has finised ?

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();
        }开发者_Go百科
        ScreenController.displayNextScreen(nextScreen);
}


The observer pattern is about notifying other objects, not other threads. Your ReadContentListener will be notified in the reading thread if the reading has finished. The original thread during this will continue it's own work (or finish, if there is none).

Using Thread.join makes the original thread simply block until the reading thread is finished. You would use this normally never directly after .start(), but only if you have other things to do on the original thread, which should be done in parallel to the reading. Calling this directly after .start() is (for the effects) equivalent to doing the reading on the original thread.


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

Using the Observer pattern is Asynchronous and, if you're doing this in UI code, is what I would use to prevent the GUI locking up (well, maybe use a SwingWorker or something like that, but the same principle).


In general it is the same, as no more statements will execute in thread, but there are points:

  • when join returns it's guaranteed that thread finished, but when notifying observer thread is still running
  • after join you are in main (or parent) thread, with observer you are in thread that is going to finish.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜