aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes avoiding a recursive deadlock Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "avoiding a recursive deadlock" Watch "avoiding a recursive deadlock" New topic
Author

avoiding a recursive deadlock

Chris IT-C
Greenhorn

Joined: Jun 03, 2001
Posts: 3
consider this example:
1 public class Man {
2 private Woman wife;
3
4 public void setSpoose(Woman newWife) {
5 wife = newWife;
6 newSpoose.setWife(this);
7 }
8 }
9 public class Woman {
10 private Man husband;
11
12 public void setSpoose(Man newHusband) {
13 husband = newHusband;
14 newSpoose.setSpoose(this);
15 }
16}
my problem consist in that I want the setSpoose-method in the class Man to call the setSpoose-method in the woman class, wich call the method in the man class and so on. How to avoid this??
regards
Christian
Chris IT-C
Greenhorn

Joined: Jun 03, 2001
Posts: 3
line 14 ofcause reads newHusband.setSpoose(this);

Christian
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Chris,

Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.

Thanks for your cooperation.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by shailesh sonavadekar (edited June 24, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Christian Staermose
Greenhorn

Joined: Jun 24, 2001
Posts: 3
See? name changed...so if anyone could advice on my problem...,please
Regards
Christian
Christian Staermose
Greenhorn

Joined: Jun 24, 2001
Posts: 3
To recap:
1 public class Man {
2 private Woman wife;
3
4 public void setSpoose(Woman newWife) {
5 wife = newWife;
6 newWife.setSpoose(this);
7 }
8 }
9 public class Woman {
10 private Man husband;
11
12 public void setSpoose(Man newHusband) {
13 husband = newHusband;
14 newHusband.setSpoose(this);
15 }
16}
my problem consist in that I want the setSpoose-method in the class Man to call the setSpoose-method in the woman class, wich call the method in the man class and so on. How to avoid this??
regards
Christian
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
In any situation, you need to set a stop reason to avoid infinate recursion. Your example lends itself to the rule (in most societies) that one man can only have one wife and one women can only have one husband. adding a test to ensure husband or wife is not null sould satisfy this rule.

Since husband sets the wife (or wife sets the husband first, you can allow the set if the other already declared you as their spouse.
In other societies, you can create a wife[] and/or a husband[] and iterate through it to see if that man or women already exists in the array and exit if it does.

------------------
Hope This Helps
Carl Trusiak, SCJP2
[This message has been edited by Carl Trusiak (edited June 24, 2001).]


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Christian Staermose
Greenhorn

Joined: Jun 24, 2001
Posts: 3
Thanks Carl. That solves it. I will be adding a divorse function, so people can change spoose
Thanks
Christian
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
I know this won't really affect the execution of your program at all, but I feel compelled to point out that the word you're looking for is "spouse", not "spoose".


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: avoiding a recursive deadlock
 
Similar Threads
How to add JPA ManyToMany records?
Object Casting Q
Behind success
access to superclass's private instance variables
abstract class