A design and coding question: If some1 can help

Discussion in 'Java' started by kaedebook, Mar 14, 2007.

  1. kaedebook

    kaedebook Guest

    Hi all,

    I got into an infinite loop problem when trying to do the following:

    // Result
    public class Result
    private List<Listener> listeners = new ArrayList<Listener>();
    private List<String> log = new ArrayList<String();

    public void addListener(Listener listener)

    public void add(String str)
    // allows the use of listener to process the result. This is
    like an observer/observable
    // pattern
    for(Listener each: listeners)

    // Task - each task has it owns result instance.
    public class Task
    private Result result = new Result();
    public Result getResult()
    return result();

    // ProblematicConstruct
    public class ProblematicConstruct implements Listener
    private Result result;
    Problem(Result result)
    this.result = result

    public void add(String s)
    //?????????? this will goes into an infinite loop ???????

    Can some one gives some advice or help on how to resolve it? Since
    there is not way to restrict people from doing the above Problematic

    kaedebook, Mar 14, 2007
    1. Advertisements

  2. I doubt it. There's not quite enough here to do anything at
    all, much less do something forever. Lucky for you, I've been
    practicing my mind-reading recently. (It's a key skill for
    customer support.)
    Notice that since neither Result nor ProblematicConstruct
    has any reference to Task, there's no way Task can have
    any influence on what's shown here.
    Your ProblematicConstruct implements listener, and contains
    an instance of Result, to which you have can add Listeners.

    Let's say you've done something (not shown here) equivalent

    And you call add("test") on that instance of ProblematicContruct.
    (Which I'll call "pc".)

    --> pc.result.add("test");
    -----> pc.add("test"); // Because pc is in results listener list.
    ---------> pc.result.add("test");
    -----------------> pc.result.add("test");

    And so on forever.

    Are you asking how you can prevent future implementors
    of Listener from making a similar mistake, and creating
    an infinite loop? You can't. You can't prevent them from
    calling Thread.stop(), either.(*)

    If you're writing a simple event handler / callback system,
    you should ignore the possiblity completely. It's not
    your problem.

    If you're building an industrial-strength plug-in system...
    maybe build something simpler first.

    More unsolicited advice: One simple way to improve the
    code you posted is to delete all the comments.

    (*) Perhaps you have a solution to this, say, involving the
    SecurityManager. I don't care.
    Mark Jeffcoat, Mar 14, 2007
    1. Advertisements

  3. kaedebook

    Lew Guest

    Y'know, I was happy to contribute an answer to this exact same question that
    you posted on clj.help as "A design and coding question: If some1 can help"
    until I saw it multiposted on clj.programmer and realized that I had wasted my
    effort. I was disappointed.

    Don't multipost. It fragments the thread and makes it impossible for people in
    one newsgroup to see answers already provided in another. It is a disincentive
    for people to answer when you multipost because it frustrates them. Cross-post

    -- Lew
    Lew, Mar 15, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.