Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Observer pattern with threads?

Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox asked on
Java
9 Comments1 Solution828 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick BarryFlag of Australia imageJava Developer
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answers