哲学者の食事問題シミュレーション(javaが書いた)

7760 ワード

[size=large]ここでは2つのアルゴリズムで、1つのchopstickクラスを共通に使用します.
Chopstickクラス:
[/size]

public class Chopstick {
/**
*
*/
private boolean enable;

/**
*
*/
public String name;

public Chopstick(boolean enable, String name) {
super();
this.enable = enable;
this.name = name;
}

public Chopstick(String name) {
this(true,name);
}

public void setEnable(boolean enbl){
this.enable = enbl;
}

public boolean getEnable(){
return this.enable;
}

/**
*
*/
public synchronized void pickup(){
try{
while(this.enable == false){
wait();
}
this.enable = false;
}catch(Exception e){

}
}

/**
*
*/
public synchronized void pickdown(){
this.enable = true;
this.notifyAll();
}
}

[size=large]Philosopherクラス[/size]

public class Philosopher extends Thread{

private String name;

Chopstick leftChopstick;
Chopstick rightChopstick;

public Philosopher(String name, Chopstick leftChopstick,
Chopstick rightChopstick) {
super();
this.name = name;
this.leftChopstick = leftChopstick;
this.rightChopstick = rightChopstick;
}

@Override
public void run() {
super.run();
leftChopstick.pickup();
System.out.println(this.name+" "+leftChopstick.name);
rightChopstick.pickup();
System.out.println(this.name+" "+rightChopstick.name);
System.out.println(this.name+" ");
try {
this.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.name+" ");
rightChopstick.pickdown();
System.out.println(this.name+" "+this.rightChopstick.name);
leftChopstick.pickdown();
System.out.println(this.name+" "+this.leftChopstick.name);

}
}

[size=large]Philosopher2[/size]

public class Philosopher2 extends Thread {

private String name;

Chopstick leftChopstick;
Chopstick rightChopstick;

public static int WITHOUTCHOPSTICK = 0;
public static int WITHONECHOPSTICK = 1;
public static int WITHTWOCHOPSTICK = 2;
public static int withChopstickStatus = 0;

public Philosopher2(String name, Chopstick leftChopstick,
Chopstick rightChopstick) {
super();
this.name = name;
this.leftChopstick = leftChopstick;
this.rightChopstick = rightChopstick;
}

@Override
public void run() {
super.run();
while (true) {
if (this.withChopstickStatus == this.WITHOUTCHOPSTICK) {
leftChopstick.pickup();
System.out.println(this.name + " " + leftChopstick.name);
this.withChopstickStatus = this.WITHONECHOPSTICK;
if (this.rightChopstick.getEnable() == true) {
rightChopstick.pickup();
System.out.println(this.name + " "
+ rightChopstick.name);
this.withChopstickStatus = this.WITHTWOCHOPSTICK;
System.out.println(this.name + " ");
try {
this.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.name + " ");
rightChopstick.pickdown();
System.out.println(this.name + " "
+ this.rightChopstick.name);
leftChopstick.pickdown();
System.out.println(this.name + " "
+ this.leftChopstick.name);
this.withChopstickStatus = this.WITHOUTCHOPSTICK;
break;
} else {
this.leftChopstick.pickdown();
System.out.println(this.name + " "
+ this.leftChopstick.name + ", "
+ this.rightChopstick.name + " ");
this.withChopstickStatus = this.WITHOUTCHOPSTICK;
try {
this.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

}

}

[size=large]diningクラス[/size]

public class Dining {

/**
* @param args
*/
public static void main(String[] args) {
Chopstick chopstick1 = new Chopstick(" 1 ");
Chopstick chopstick2 = new Chopstick(" 2 ");
Chopstick chopstick3 = new Chopstick(" 3 ");
Chopstick chopstick4 = new Chopstick(" 4 ");
Chopstick chopstick5 = new Chopstick(" 5 ");

Philosopher philosopher1 = new Philosopher(" 1 ", chopstick5,chopstick1);
Philosopher philosopher2 = new Philosopher(" 2 ", chopstick1,chopstick2);
Philosopher philosopher3 = new Philosopher(" 3 ", chopstick2,chopstick3);
Philosopher philosopher4 = new Philosopher(" 4 ", chopstick3,chopstick4);
Philosopher philosopher5 = new Philosopher(" 5 ", chopstick4,chopstick5);
long startTime = System.currentTimeMillis();
ArrayList threads = new ArrayList();
threads.add(philosopher1);
threads.add(philosopher2);
threads.add(philosopher3);
threads.add(philosopher4);
threads.add(philosopher5);
philosopher2.start();
philosopher4.start();
philosopher1.start();
philosopher3.start();
philosopher5.start();
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(System.currentTimeMillis() - startTime);
}

}

[size=large]Dining 2類[/size]

public class Dining2 {

/**
* @param args
*/
public static void main(String[] args) {
Chopstick chopstick1 = new Chopstick(" 1 ");
Chopstick chopstick2 = new Chopstick(" 2 ");
Chopstick chopstick3 = new Chopstick(" 3 ");
Chopstick chopstick4 = new Chopstick(" 4 ");
Chopstick chopstick5 = new Chopstick(" 5 ");

Philosopher2 philosopher1 = new Philosopher2(" 1 ", chopstick5,chopstick1);
Philosopher2 philosopher2 = new Philosopher2(" 2 ", chopstick1,chopstick2);
Philosopher2 philosopher3 = new Philosopher2(" 3 ", chopstick2,chopstick3);
Philosopher2 philosopher4 = new Philosopher2(" 4 ", chopstick3,chopstick4);
Philosopher2 philosopher5 = new Philosopher2(" 5 ", chopstick4,chopstick5);
long startTime = System.currentTimeMillis();
ArrayList threads = new ArrayList();
threads.add(philosopher1);
threads.add(philosopher2);
threads.add(philosopher3);
threads.add(philosopher4);
threads.add(philosopher5);
philosopher2.start();
philosopher4.start();
philosopher1.start();
philosopher3.start();
philosopher5.start();
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(System.currentTimeMillis() - startTime);
}

}

[size=large]
5人の哲学者が同時に箸を持ち、同時に待つか、同時に置くのを防ぐためだ.哲学者2と4を先にすることができます.
[/size]