我的 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 时,它也成功运行。
我的问题是使用 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();
}
}