How can I achieve this?

Posted on 2004-04-25
Last Modified: 2010-03-31
I have a class "task", which has a method called "evaluate()". When this method is called and there is a state change, this Task will notify other Tasks. (The other tasks would in turn evaluate and notify some other tasks if evaluation return true).

my preliminary idea is to use thread's notification methods. But is there any other easier or more neat way to accomplish this?

Question by:ggyao2003

Accepted Solution

mmuruganandam earned 125 total points
ID: 10915942
You have Observer and Observable

Have a look at this... for more information..
LVL 13

Expert Comment

ID: 10916600
Hi ggyao2003,

You can define a listener interface, for example:

    public interface TaskListener
         public void notifiedByTask(Task source  /* , ... other needed parameters */ );

And implements this interface in your task class:

    public class Task
    implements TaskListener
          public void notifiedByTask(Task source  /* , ... other needed parameters */ )

          private Vector listeners=new Vectors(); // keep listener references

          public void addTaskListener(TaskListener tl)
                if (!listeners.contains(tl)) listeners.add(tl);

          public void removeTaskListener(TaskListener tl)

          private void notifyListeners()
                int nb=listeners.size();
                for (int i=0;i<nb;i++)
                      ( (TaskListener)listeners.elementAt(i) ).notifiedByTask(this, ... );

LVL 23

Expert Comment

ID: 10916671
try here you will get lot of stuff...


Author Comment

ID: 10922643
webstorm's advice is the easiest to implement in my case. It is really really simple.

Author Comment

ID: 10922672
I made a mistake in my previous post. It should be "mmuruganandam" instead of "webstorm".
But webstorm did gave a great suggestion!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 51
servlet doXXX methods 3 61
HashTable highest marks enumeration alternative 9 42
collection output issue 9 36
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question