1.需要写一个筷子类(Chopstick)
-->这个筷子对象每次被new出来后,内存中有一个计数器随后增加,以证明这根筷子是哪根筷子。
-->重写toString()因为要打印这个类对象时要输出一些必要信息。
2. 需要写一个哲学家类(Philospher)这个类extends Thread这个类
-->它有3个类属性 3个普对象属性 (随机器,计数器,思考需要的时间,左筷子,右筷子,
-->它有5个方法(构造方法,思考方法,吃方法,toString(),run())
3.需要写个哲学家进餐类,主要是程序的入口没其它的意思。
Notice:
程序运行需注意:需要至少3个参数否则程序终止,并打印错误信息
first argument:Philospher Number
second argument:think time (please number)
third argument:please “deadlock” otherwise program will continue run.
fouth argument(option argument):input time(seconds) ,will specify time after stop program.
1.需要写一个筷子类(Chopstick)
-->这个筷子对象每次被new出来后,内存中有一个计数器随后增加,以证明这根筷子是哪根筷子。
-->重写toString()因为要打印这个类对象时要输出一些必要信息。
2. 需要写一个哲学家类(Philospher)这个类extends Thread这个类
-->它有3个类属性 3个普对象属性 (随机器,计数器,思考需要的时间,左筷子,右筷子,
-->它有5个方法(构造方法,思考方法,吃方法,toString(),run())
3.需要写个哲学家进餐类,主要是程序的入口没其它的意思。
Notice:
程序运行需注意:需要至少3个参数否则程序终止,并打印错误信息
first argument:Philospher Number
second argument:think time (please number)
third argument:please “deadlock” otherwise program will continue run.
fouth argument(option argument):input time(seconds) ,will specify time after stop program.
class Chopstick{
private static int counter = 0;
private int number = counter++;
public String toString()
{
return "Chopstick " + number;
}
}
class Philosopher extends Thread{
private static Random rand = new Random();
private static int counter = 0;
private int number = counter++;
private Chopstick leftChopstick;
private Chopstick rightChopstick;
static int ponder = 0;
public Philosopher(Chopstick left,Chopstick right)
{
leftChopstick = left;
rightChopstick = right;
start();
}
public void think()
{
System.out.println(this + " thinking");
if(ponder > 0)
{
try {
sleep(rand.nextInt(ponder));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public void eat()
{
synchronized(leftChopstick)
{
System.out.println(this + " has " + this.leftChopstick + " Waiting for "
+ this.rightChopstick);
synchronized(rightChopstick)
{
System.out.println(this + " eating");
}
}
}
public String toString()
{
return "Philosopher " + number;
}
@Override
public void run() {
while(true)
{
think();
eat();
}
}
}
public class DiningPhilosophers {
public static void main(String[] args) {
args = new String[]{"5","5000","deadlock"};
if (args.length < 3) {
System.err.println("需输入至少3个参数!\n"
+ "第一个参数:哲学家人数\n"
+ "第二个参数:思考时间\n"
+ "第三个参数:输入'deadlock'否则不会死锁\n"
+ "第四个参数:指定时间(seconds)到后会停止程序.\n");
System.exit(0);
}
Philosopher[] philosopher =
new Philosopher[Integer.parseInt(args[0])];
Philosopher.ponder = Integer.parseInt(args[1]);
Chopstick
left = new Chopstick(),
right = new Chopstick(),
first = left;
int i = 0;
while(i < philosopher.length - 1)
{
philosopher[i++] = new Philosopher(left,right);
left = right;
right = new Chopstick();
}
if(args[2].equals("deadlock"))
philosopher[i] = new Philosopher(left,first);
else
philosopher[i] = new Philosopher(first,left);
//Optionally break out of program:
if(args.length >= 4){
int delay = Integer.parseInt(args[3]);
if(delay != 0)
new Timeout(delay * 1000,"Timed out");
}
}
}
public class Timeout extends Timer {
public Timeout(int delay,final String msg)
{
super(true);//Daemon thread
schedule(new TimerTask(){
@Override
public void run() {
System.out.println(msg);
System.exit(0);
}
},delay);
}
}