我的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;
}
}
此代码成功运行。当我使用Semaphore而不是布尔值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();
}
}
答案 0 :(得分:0)
我想你要找的是Conductor solution的变体。
你的互斥体将作为指挥,但一次只允许一位哲学家采取分叉和用餐。
因此任何phislopher线程都会等待互斥锁上的成功acquire()
,取得必须可用的分叉,因为没有其他哲学家正在用餐。然后用餐,放下叉子,并在互斥锁上拨打release()
。