Hi, ich hoffe dass das ein blöder Flüchtigkeitsfehler ist, aber ich komm einfach nicht drauf was an meinem Bsp. falsch sein soll. Ich hab mir sogar die "Lösung" von SUN angeschaut, und meiner Ansicht nach sollten meine Klassen ident sein, aber sie arbeiten nur mit anderen Objekten.
Ich habe mich noch -nicht- mit dem Deadlockproblem beschäftigt, mich interessiert einzig warum bei mir immer nur 2 Philosophers essen, und der Rest wartet. Wenn ich im Debugger durchgehe rennt er mir ordentlich in die mit notify() bestückte put() Methode, aber ab dem Zeitpunkt weiß ich nicht mehr warum es nicht funktioniert.
spoon = 0 <-- spoon wird gerade von jemandem benutzt
spoon = 1 <-- frei verfügbar, jemand kann ihn sich holen
[highlight=Java]
public class Table {
private static final int MAX_SIZE = 5;
private Integer[] spoons;
public int size = 0;
public Table(int size)
{
this.size = size;
this.spoons = new Integer[size%(MAX_SIZE+1)];
for (int i = 0; i< size;++i)
{
spoons = 1;
}
}
public int getSpoon(int position) throws InterruptedException
{
while (spoons[position]==0)
{
synchronized(spoons[position])
{
//spoon taken, wait for it to come back
spoons[position].wait();
}
}
synchronized(spoons[position])
{//spoon is not taken anymore, so take it!
spoons[position]=new Integer(0);
return 1;
}
}
public void putSpoon(int position)
{
synchronized(spoons[position])
{
//spoon available again
spoons[position]=new Integer(1);
//wake a waiting philosopher thread up, if he is currently waiting for spoon.
spoons[position].notify();
}
}
}[/highlight]
[highlight=Java]
public class Philosopher extends Thread {
public boolean running = false;
public int sittingPos = 0;
// a philosopher only eats with 2 spoons equipped!
int[] cutlery = new int[2];
Table spoons;
public boolean eating = false;
public boolean thinking = true;
public boolean waiting = false;
public Philosopher(int sittingPos,Table spoons)
{
this.spoons = spoons;
this.sittingPos = sittingPos;
this.start();
}
public void run()
{
this.running = true;
while (running)
{
waiting = true;
thinking = false;
eating = false;
try
{
// try obtaining the first spoon
cutlery[0] = spoons.getSpoon(sittingPos);
}catch(InterruptedException e)
{
e.printStackTrace();
}
try
{
// try obtaining the second spoon
cutlery[1] = spoons.getSpoon((sittingPos+1) % spoons.size);
}catch(InterruptedException e)
{
e.printStackTrace();
}
if (cutlery[0]==1 && cutlery[1]==1)
{
this.eating = true;
this.waiting = false;
this.thinking = false;
try {
this.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//finished eating for a while, put the spoons back
if (cutlery[0]==1)
{
spoons.putSpoon(sittingPos);
cutlery[0]=0;
}
if (cutlery[1]==1)
{
spoons.putSpoon((sittingPos+1) % spoons.size);
cutlery[1]=0;
}
// think for a while before getting hungry again
this.eating = false;
this.waiting = false;
this.thinking = true;
try {
this.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void stopPhilosopher()
{
running = false;
}
public void printStatus()
{
if (eating)
System.out.print(sittingPos+" eating.");
else if (waiting)System.out.print(sittingPos+" waiting.");
else System.out.print(sittingPos+" thinking.");
}
}
[/highlight]
Ich habe mich noch -nicht- mit dem Deadlockproblem beschäftigt, mich interessiert einzig warum bei mir immer nur 2 Philosophers essen, und der Rest wartet. Wenn ich im Debugger durchgehe rennt er mir ordentlich in die mit notify() bestückte put() Methode, aber ab dem Zeitpunkt weiß ich nicht mehr warum es nicht funktioniert.
spoon = 0 <-- spoon wird gerade von jemandem benutzt
spoon = 1 <-- frei verfügbar, jemand kann ihn sich holen
[highlight=Java]
public class Table {
private static final int MAX_SIZE = 5;
private Integer[] spoons;
public int size = 0;
public Table(int size)
{
this.size = size;
this.spoons = new Integer[size%(MAX_SIZE+1)];
for (int i = 0; i< size;++i)
{
spoons = 1;
}
}
public int getSpoon(int position) throws InterruptedException
{
while (spoons[position]==0)
{
synchronized(spoons[position])
{
//spoon taken, wait for it to come back
spoons[position].wait();
}
}
synchronized(spoons[position])
{//spoon is not taken anymore, so take it!
spoons[position]=new Integer(0);
return 1;
}
}
public void putSpoon(int position)
{
synchronized(spoons[position])
{
//spoon available again
spoons[position]=new Integer(1);
//wake a waiting philosopher thread up, if he is currently waiting for spoon.
spoons[position].notify();
}
}
}[/highlight]
[highlight=Java]
public class Philosopher extends Thread {
public boolean running = false;
public int sittingPos = 0;
// a philosopher only eats with 2 spoons equipped!
int[] cutlery = new int[2];
Table spoons;
public boolean eating = false;
public boolean thinking = true;
public boolean waiting = false;
public Philosopher(int sittingPos,Table spoons)
{
this.spoons = spoons;
this.sittingPos = sittingPos;
this.start();
}
public void run()
{
this.running = true;
while (running)
{
waiting = true;
thinking = false;
eating = false;
try
{
// try obtaining the first spoon
cutlery[0] = spoons.getSpoon(sittingPos);
}catch(InterruptedException e)
{
e.printStackTrace();
}
try
{
// try obtaining the second spoon
cutlery[1] = spoons.getSpoon((sittingPos+1) % spoons.size);
}catch(InterruptedException e)
{
e.printStackTrace();
}
if (cutlery[0]==1 && cutlery[1]==1)
{
this.eating = true;
this.waiting = false;
this.thinking = false;
try {
this.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//finished eating for a while, put the spoons back
if (cutlery[0]==1)
{
spoons.putSpoon(sittingPos);
cutlery[0]=0;
}
if (cutlery[1]==1)
{
spoons.putSpoon((sittingPos+1) % spoons.size);
cutlery[1]=0;
}
// think for a while before getting hungry again
this.eating = false;
this.waiting = false;
this.thinking = true;
try {
this.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void stopPhilosopher()
{
running = false;
}
public void printStatus()
{
if (eating)
System.out.print(sittingPos+" eating.");
else if (waiting)System.out.print(sittingPos+" waiting.");
else System.out.print(sittingPos+" thinking.");
}
}
[/highlight]