Monday, February 21, 2005

Dining Philosophers and XP : Part 2: The first Iteration

hey, the title seems like a Star Wars movies' one.

So I went ahead and implemented all the features to be implemented in Iteration 1. I also did the factory implementation as I wanted to use it as the framework for holding the relationship between philosophers. Is this done normally?

I basically have a Factory for making Philosophers, a Philosopher class which extends the Thread method and a Chopstick class and corresponding Test classes. I am planning to use the new utils.concurrent package (J2SE 1.5 aka 'Tiger') to implement threading in the 4th Iteration. As expected Deadlock happends. This will be removed in the next round. I think the code 'smells' with methods like:

public boolean setRightChopstick() {
int id = (Math.abs(getPhilosopherId() + 1) % 5);

if (PhilosopherFactory.getPhilosopherById(id).hasLeftChopstick() == true
&& PhilosopherFactory.getPhilosopherById(id).getLeftChopstick()
.isInUse() == false) {

PhilosopherFactory.getPhilosopherById(getPhilosopherId()).hasRightChopstick = true;
csRight = PhilosopherFactory.getPhilosopherById(id)
.getLeftChopstick();
PhilosopherFactory.getPhilosopherById(id).relinquishLeftChopstick();
PhilosopherFactory.getPhilosopherById(id).setHasLeftChopstick(false);
csRight.setOwnerPhilosopher(this);
return true;
}
return false;
}
What do you guys think about this?

I collaborated for the most part with my bro' Krishnan (aka 'kannan') on this round and he also plans to come with me for the Agile India conference. I want to release the whole code through a public URL of a .zip file. What is the best way to do this? The most I could come up with was as .zip file hosted in geocities.com site.

Code is here

2 Comments:

At 3:40 PM, Anonymous Rickard Johansson said...

Some hints that perhaps can help you make the code a little shorter and clearer.

The code:
if (someStatement==true) {...

can (and should) be written as

if (someStatement) {...

The code:

if (someStatement) {
return true;
} else {
return false;
}

is equal to just writing

return someStatement;

 
At 8:00 PM, Blogger sundar said...

Thanks Rickard. I will definitly do as you have suggested in the next iteration.

 

Post a Comment

<< Home