Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

Observer pattern with threads?

Hi,

How does the observer pattern work with two threads? For example, I have my main thread, then a background thread which needs to notify the main thread of some event. Something like:


public class MyApp implements INotify
{
     BackgroundThread m_thread;

     public MyApp()
     {
         m_thread = new BackgroundThread(this);
         m_thread.start();
     }
 
     public void notifyMe(String str)
     {
           System.out.println("Background thread says: " + str);
     }
}

public class BackgroundThread implements Runnable
{
     INotify m_observer;

    public BackgroundThread(INotify observer)
    {
        m_observer = observer;
    }

    public void run()
    {
         while (true) {
             m_observer.notifyMe("hello!");
             Thread.Sleep(60 * 1000);
        }
    }
}

I must be using the wrong search terms because I can't find anything that shows how to do this, just large messaging APIs between remote servers and clients. But I just want to implement this within an application, so some classes can subscribe to a thread for message notifications. I've been searching for java + asynchronous notification etc.

Also, this needs to be j2me compliant which for me means java 1.3,


Thanks
Avatar of sciuriware
sciuriware

If the main thread must wait for the notification, then the wakeup and the wait
should be performed in synchronised parts of the code to avoid race conditions.

;JOOP!
Avatar of DJ_AM_Juicebox

ASKER

>> If the main thread must wait for the notification

No the main thread doesn't need to wait, it just needs to receive the notification without crashing the system.

I still don't understand how the above works?
It doesn't.
If the main thread is not waiting it must be polling some static variable to get
any message.
Another way is to call a method in the main thread's classes, but that's fooling:
the running thread is then still the other.

;JOOP!
>> If the main thread is not waiting it must be polling some static variable to get
any message.

Yeah I'm trying to avoid polling from the main thread.


>> Another way is to call a method in the main thread's classes, but that's fooling:
the running thread is then still the other.

Yeah you mean if I call a method running in the main thread from the background thread, I'm really still in the context of the background thread.

So how do all these libraries work like messaging and such where you register for some asynchronous event and you get it safely in the main thread? Is the main thread really polling?

Thanks
By:
1) sleeping on an event and to be awoken by another thread.
2) by the way I described above; the classical example is the 'actionPerformed()'
method that is run from the event thread.

You must understand that a running, not waiting thread, can't be kicked or signaled;
even not killed.

;JOOP!
Sorry, I still don't get it.

>> 1) sleeping on an event and to be awoken by another thread.
In my case, I'd want the main thread to sleep, and the background thread to wake it when there is a message to be processed by the main thread. But this doesn't make any sense because if I put the main thread to sleep, the user won't be able to interact with the application.


>> 2) by the way I described above; the classical example is the 'actionPerformed()'
method that is run from the event thread.
This is where the background thread calls a method on a class created in the main thread to process the message - but still the same problem, this call isn't synchronized with the main thread?


>> You must understand that a running, not waiting thread, can't be kicked or signaled; even not killed.

That  I understand, so I guess I'm making some progress!

Thanks
1) of course (s)he can: press a button and the event thread wakes up.
2) the event thread must not sleep, indeed, or all your controls are dead.
So it must wake up other threads.

Progress? What else do you think we are here for?

;JOOP!
I should be sleeping already. See you tomorrow.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial