A design and coding question: If some1 can help

K

kaedebook

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)
{
list.add(listener);
}

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

// 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 ???????
result.add(s);
}
}

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
construct.

Thanks,
Kae
 
M

Mark Jeffcoat

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

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.)
// Result
public class Result
{
private List<Listener> listeners = new ArrayList<Listener>();
private List<String> log = new ArrayList<String();

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

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

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

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.
// 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 ???????
result.add(s);
}
}

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
to
this.result.add(this);

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

pc.add("test");
--> 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.


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
construct.

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.
 
L

Lew

Hi all,

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

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
instead.

-- Lew
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top