I think i have understood the basic locks , synchronized statements and methods etc etc . I would like to actually program something that used all this. Something that is prone to breaking if one is not careful , also would love if there is swing involved ( i read that swing has a lot of relation with java concurrency , but that is almost all i know of ). Can someone suggest some project focused around concurrency ? I know there is a projects sticky , but there were so many options there and i didnt know which one to go with.

Recommended Answers

All 4 Replies

Dining Philosophers is a lot of fun, and trickier than it looks. It's a classic where the solution is hard to wrap your brain around, but really not so hard once you've got it.
Strongly recommended for building your threading qi

commented: thanks a lot ! +5

I am trying it out , However a question :
The wiki talks about the Chandy-Misra solution , in the 3rd point of that algorithm , it says :

When a philosopher with a fork receives a request message, he keeps the fork if it is clean, but gives it up when it is dirty. If he sends the fork over, he cleans the fork before doing so

Why does this not lead to a deadlock ? if one philosopher has one clean fork , and waits to get another clean fork from a neighboring diner/philosopher , who in turn is waiting for a fork as well , this can cumulate to a deadlock right ? one philosopher always waiting for a fork from the other ?

update :

However, if the system is initialized to a perfectly symmetric state, like all philosophers holding their left side forks, then the graph is cyclic at the outset, and their solution cannot prevent a deadlock.

Okay , so i guess that is a caveat , and

Initializing the system so that philosophers with lower IDs have dirty forks ensures the graph is initially acyclic.

This is the workaround , However , I'm not sure about how to do this part in code. till now , i have a immutable fork class .

public class Fork {

    public final boolean isClean;
    public final int id;

    public Fork(int id , boolean isClean){
        this.id = id;
        this.isClean = isClean;
    }
}

perhaps this should all be in a new thread ?

Execution goes in threads, classes don't.
isClean will change, so it can't be final and the class can't be immutable

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.