You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by Christian Asmussen <kr...@kriconet.com.br> on 2002/02/16 20:05:36 UTC

Recursion -> Possible infinite loop

I got across a problem, and would like to know if you guys have a formal
way of dealing with it.  Supose we have the following recursive method

public int getDepth(){
	return getParent() == null ? 0 : getParent().getDepth() + 1;
}


If an interface disigner creates an object with "setParent(this)" then
getDepth would cause an infinite loop.  Depending on the method, this
could cause a whole server to go OOME.  Whats the best for to deal whit
this?  I thought of three ways of avoiding it.  What's the best one?

1 - Simply avoid recursion

2 - Something like semaphores

boolean recursing = false;
public int getDepth(){
	if(recursing) throw new RuntimeException("DeadLock");
	recursing = true;
	int ret = 0;
	if(getParent() != null)
	  ret = 1 + getParent().getDepth();
	recursing = false;
	return ret;
}

3 - Keep some kinda graph (vertices are conected by getDepth()), something
like:

public void setParent(ThisObject o){
	if(formsCycle(o)) throw new Exception("Cycle detected");
...
}


Maybe I should have sent this message to the user list?  If so, sorry.

Tks
	

-- 
"If we did all the things we are capable of, 
we would literally astound ourselves"
 - Thomas Edison



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Recursion -> Possible infinite loop

Posted by Daniel Rall <dl...@finemaltcoding.com>.
Christian Asmussen <kr...@kriconet.com.br> writes:

> On 16 Feb 2002, Jason van Zyl wrote:
>
>> On Sat, 2002-02-16 at 14:05, Christian Asmussen wrote:
>> > I got across a problem, and would like to know if you guys have a formal
>> > way of dealing with it.  Supose we have the following recursive method
>> > 
>> > public int getDepth(){
>> > 	return getParent() == null ? 0 : getParent().getDepth() + 1;
>> > }
>> > 
>> > 
>> > If an interface disigner creates an object with "setParent(this)" then
>> > getDepth would cause an infinite loop.  Depending on the method, this
>> > could cause a whole server to go OOME.  Whats the best for to deal whit
>> > this?  I thought of three ways of avoiding it.  What's the best one?
>> 
>> If in this particular case you required an acyclic graph of objects then
>> you could either use something like a class invariant or an assertion to
>> make sure that the parameter to setParent(Object o) isn't itself.
>
> But what if the guy goes getParent().setParent(this);  or more?  Ooops you
> sayd acyclic right ;-)  But I assume it is the developers task to avoid
> this right?

An IllegalArgumentException or subclass could be thrown.  Submit a
patch:

http://jakarta.apache.org/site/source.html


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Recursion -> Possible infinite loop

Posted by Christian Asmussen <kr...@kriconet.com.br>.
On 16 Feb 2002, Jason van Zyl wrote:

> On Sat, 2002-02-16 at 14:05, Christian Asmussen wrote:
> > I got across a problem, and would like to know if you guys have a formal
> > way of dealing with it.  Supose we have the following recursive method
> > 
> > public int getDepth(){
> > 	return getParent() == null ? 0 : getParent().getDepth() + 1;
> > }
> > 
> > 
> > If an interface disigner creates an object with "setParent(this)" then
> > getDepth would cause an infinite loop.  Depending on the method, this
> > could cause a whole server to go OOME.  Whats the best for to deal whit
> > this?  I thought of three ways of avoiding it.  What's the best one?
> 
> If in this particular case you required an acyclic graph of objects then
> you could either use something like a class invariant or an assertion to
> make sure that the parameter to setParent(Object o) isn't itself.

But what if the guy goes getParent().setParent(this);  or more?  Ooops you
sayd acyclic right ;-)  But I assume it is the developers task to avoid
this right?

 -- 
"If we did all the things we are capable of, 
we would literally astound ourselves"
 - Thomas Edison


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Recursion -> Possible infinite loop

Posted by Jason van Zyl <jv...@zenplex.com>.
On Sat, 2002-02-16 at 14:05, Christian Asmussen wrote:
> I got across a problem, and would like to know if you guys have a formal
> way of dealing with it.  Supose we have the following recursive method
> 
> public int getDepth(){
> 	return getParent() == null ? 0 : getParent().getDepth() + 1;
> }
> 
> 
> If an interface disigner creates an object with "setParent(this)" then
> getDepth would cause an infinite loop.  Depending on the method, this
> could cause a whole server to go OOME.  Whats the best for to deal whit
> this?  I thought of three ways of avoiding it.  What's the best one?

If in this particular case you required an acyclic graph of objects then
you could either use something like a class invariant or an assertion to
make sure that the parameter to setParent(Object o) isn't itself.

-- 
jvz.

Jason van Zyl
jvanzyl@apache.org

http://tambora.zenplex.org


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>