哲学家进餐问题:(参考think in java third version –author Bruce Eckel)

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);
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值