私のJavaプロジェクトには3つのJavaクラスがあります。Main、Philosoper、chStickがあります。ブール値isTakenを使用した食事する哲学者の問題を解決しました。
以下のようなプロジェクトchStick.javaで。このクラスは、使用中または使用しない箸を制御します。
class ChStick
{
private int id;
private boolean isTaken;
ChStick (int id)
{
this.id=id;
}
synchronized void get () throws InterruptedException
{
while(isTaken){
wait();
}
isTaken=true;
}
int getID ()
{
return id;
}
synchronized void put () throws InterruptedException
{
isTaken=false;
}
}
このコードは正常に機能します。ブール値isTakenの代わりにSemaphoreを使用すると、正常に機能します。
私の問題は、Mutexを使用してこのクラスを書き直すことです。私はたくさんの方法を試しましたが、それでもうまくいきませんでした。Mutexクラスを使用する必要があります。
編集:
class Philos extends Thread// #9 - Make this class to be inherited from Thread class
{
private String name;
private ChStick left, right;
Philos (String name, ChStick left, ChStick right)
{
// #10 - Assign this philosopher a name, his left and rigt chopsticks.
this.left=left;
this.right=right;
this.name=name;
}
@Override
public void run ()
{
while (true)
{
try
{
System.out.println ("Philosopher " + name + " is thinking.");
// #11 - Make philosopher sleep for a RANDOM time.
Thread.sleep(1000);
System.out.println ("Philosopher " + name + " is hungry.");
if (left.getID () < right.getID ())
{
System.out.println ("Philosopher " + name +
" getting left ChStick.");
// #12 - Make philosopher to get left chopstick.
left.get();
System.out.println ("Philosopher " + name +
" got left ChStick."+left.getID());
}
else
{
System.out.println ("Philosopher " + name +
" getting right ChStick.");
// #13 - Make philosopher to get right chopstick.
right.get();
System.out.println ("Philosopher " + name +
" got right ChStick."+right.getID());
}
// Get other ChStick.
if (left.getID () < right.getID ())
{
System.out.println ("Philosopher " + name +
" getting right ChStick.");
// #14 - Make philosopher to get (left or right)? chopstick.
right.get();
System.out.println ("Philosopher " + name +
" got right ChStick."+right.getID());
}
else
{
System.out.println ("Philosopher " + name +
" getting left ChStick.");
// #15 - Make philosopher to get (left or right)? chopstick.
left.get();
System.out.println ("Philosopher " + name +
" got left ChStick."+left.getID());
}
System.out.println ("Philosopher " + name + " is eating.");
// #16 - Make philosopher sleep for a RANDOM time.
Thread.sleep(2000);
// #17 - Make philosopher to put the left chopstick down.
System.out.println ("Philosopher " + name +
" putting down left ChStick."+left.getID());
left.put();
// #17 - Make philosopher to put the right chopstick down.
System.out.println ("Philosopher " + name +
" putting down right ChStick."+right.getID());
right.put();
}
catch (InterruptedException e) {}
}
}
}
public class Main
{
public static void main (String [] args)
{
// #1 - Here you should create chopstick instances
// and number them using the structure defined in its class definition.
// ChStick cs1 = new ChStick ..
ChStick cs1 = new ChStick(1);
ChStick cs2 = new ChStick(2);
ChStick cs3 = new ChStick(3);
ChStick cs4 = new ChStick(4);
ChStick cs5 = new ChStick(5);
// #2 - Here you should create Philosopher instances
// and assign them names, their right and left chopsticks.
// BE CAREFUL IN WHICH ORDER THEY SIT AROUND THE TABLE
// Philos p1 = new Philos ("P1"...)
Philos p1 = new Philos("P1",cs1,cs5);
Philos p2 = new Philos("P2",cs2,cs1);
Philos p3 = new Philos("P3",cs3,cs2);
Philos p4 = new Philos("P4",cs4,cs3);
Philos p5 = new Philos("P5",cs5,cs4);
// #3 - Start p
p1.start();
p2.start();
p3.start();
p4.start();
p5.start();
}
}