+2  A: 

This is from the edit to my answer to your first question after you added this information:

"EDIT: The while loop is an infinite loop causing a for loop to run in which every item in an array has been checked to see if its been reset. You can replace this with an observer pattern where when an object is reset, it notifies the observing object which then performs that set of steps. This way you don't have an infinite loop and you cut down on the usage of .isReset()."

EDIT: The problem with the other answers is that its a bit of a code smell to use an infinite loop in my opinion.

EDIT 2: Here's the wikipedia article with an example.

indyK1ng
I agree with you. The observer pattern is definitely the way to go.
Matthew Flaschen
do you have any example of how to do this?
ferronrsmith
+5  A: 

Nothing wrong with that isReset, but won't this loop just call it over and over and over (and over and over...)?

 while (true) {
     for (i=0; i < alg.length; i++) 
        if (execPanel[i].isReset()) {
           alg[i].terminate();
           createProgram(i);
           alg[i].start();
           execPanel[i].unreset();
        }         
  }

If nothing else, you can slap a Thread.yield() in there to force the thread to give up its time slice regularly so it doesn't hog the CPU.

Better would be to ditch the polling and instead use a listener-based approach. Allow the execPanels to notify listeners when they are reset instead of having one loop repeatedly ask them if they are reset. This code is the equivalent of "Are we there yet? Are we there yet? Are we there yet?"

You might also see listeners referred to as observers. If you look around for more information about this design pattern a lot of good information will turn up using both terms.

Example

Here's how you might rewrite your code to use the listener concept. I apologize for any errors in this code, I haven't compiled it. Hopefully the ideas are clear despite any errors I might have in the code.

If the code doesn't make sense right away, check out my links above. Or you can play around with EventListeners, which you'll use all the time when you do Swing GUI programming.

public class SkaExecutionPanel {
    // Anybody who wants to be told when a panel is reset will implement
    // this interface and call addListener().
    public interface Listener {
        void panelReset(SkaExecutionPanel panel);
    }

    // Each panel needs to maintain an internal list of listeners.
    private List<Listener> listeners = new ArrayList<Listener>();

    public void addListener(Listener listener) {
        listeners.add(listener);
    }

    public void removeListener(Listener listener) {
        listeners.remove(listener);
    }

    public void reset() {
        /* Do your thing. */

        // When something happens that the listeners will care about, such as a
        // call to reset() in this case, you iterate through the list and tell
        // each one what's happened.
        for (Listener listener: listeners) {
            listener.panelReset(this);
        }
    }
}

public class SkaTest {
    public static void main(String args[]) {
        /* Snippety snip. */

        for (i=0; i < alg.length; i++) {
            // Here we need to tell the execPanels that we want to do something
            // whenever somebody calls reset() on them. We will add a listener
            // to each panel that does the terminate/createProgram/start dance.
            // This way we don't have to ask the panels when they are reset; 
            // instead they will tell us when that happens.
            execPanel[i].addListener(new SkaExecutionPanel.Listener() {
                final int index = i;

                public void panelReset(SkaExecutionPanel panel) {
                    alg[index].terminate();
                    createProgram(index);
                    alg[index].start();
                    execPanel[index].unreset();
                }
            });
        }
    }
}
John Kugelman
Can you give an example of how you would implement this?
ferronrsmith
Sure, I added some example code. I didn't actually compile it so others will need to tell me if I messed something up.
John Kugelman