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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 52
running on tomcat not jboss eap 7.0 3 28
Setup GlassFish 4 34
Find out all repos that a user is most active on Github 1 26
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

679 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