Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

A design and coding question: If some1 can help

Thread view: 
kaedebook@gmail.com - 14 Mar 2007 23:17 GMT
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
Mark Jeffcoat - 15 Mar 2007 00:52 GMT
> 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
[quoted text clipped - 29 lines]
>    }
> }

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
[quoted text clipped - 11 lines]
>    }
> }

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.

Signature

Mark Jeffcoat
Austin, TX

Lew - 15 Mar 2007 04:41 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.