本文介绍了餐饮哲学家,但十个哲学家.我想更改此解决方案.我想完成这段代码.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
10位哲学家进行至少5次饮食和思考交易,并使他们变得充实.(使用Java)
10 philosophers eating and thinking transaction at least 5 times and make them to be full.(with java)
package diningphilosophers;
public class DiningPhilosophers {
public static void main(String[] args) {
Table table = new Table();
for(int i = 0; i < <strong class="highlight">10</strong>; i++) {
Philosopher newGuy = new Philosopher(i);
table.sitAt(newGuy);
Thread starter = new Thread(newGuy);
starter.start();
}
}
}---------------------------------------
package diningphilosophers;
public class Fork {
Philosopher heldBy;
public Fork grabFork(Philosopher grabber) {
if(heldBy == null) {
heldBy = grabber;
return this;
}
return null;
}
public boolean releaseFork(Philosopher releaser) {
if(heldBy.equals(releaser)) {
heldBy = null;
return true;
}
return false;
}
public boolean isAvailable() {
if(heldBy == null) {
return true;
}
return false;
}
}--------------------------------------------------------------
package diningphilosophers;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Philosopher implements Runnable{
private Fork leftFork, rightFork;
private Table atTable;
private int tablePosition;
private boolean isFull;
private Random randomGenerator = new Random();
public Philosopher(int tablePosition) {
this.tablePosition = tablePosition;
leftFork = null;
rightFork = null;
isFull = false;
}
@Override
public void run() {
for(int i = 0; i < <strong class="highlight">5</strong>; i++) {
<strong class="highlight">think</strong>();
<strong class="highlight">eat</strong>();
}
isFull = true;
System.out.println("Philosopher " + tablePosition + " is done!");
}
private void <strong class="highlight">think</strong>() {
System.out.println("Philosopher " + tablePosition + " is thinking...");
try {
Thread.sleep(randomGenerator.nextInt(500));
} catch (InterruptedException ex) {
Logger.getLogger(Philosopher.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void setTable(Table table) {
atTable = table;
}
private void <strong class="highlight">eat</strong>() {
getLeftFork();
System.out.println("Philosopher " + tablePosition + " got his left fork!");
getRightFork();
System.out.println("Philosopher " + tablePosition + " got his right fork!");
System.out.println("Philosopher " + tablePosition + " is eating...");
try {
Thread.sleep(randomGenerator.nextInt(500));
} catch (InterruptedException ex) {
Logger.getLogger(Philosopher.class.getName()).log(Level.SEVERE, null, ex);
}
releaseForks();
}
private void getLeftFork() {
while (true) {
leftFork = atTable.getLeftFork(tablePosition);
if(leftFork != null) {
return;
}
else {
try {
Thread.sleep(randomGenerator.nextInt(100));
} catch (InterruptedException ex) {
Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
private void getRightFork() {
while (true) {
rightFork = atTable.getLeftFork(tablePosition);
if(rightFork != null) {
return;
}
else {
try {
Thread.sleep(randomGenerator.nextInt(100));
} catch (InterruptedException ex) {
Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
private void releaseForks() {
leftFork = null;
rightFork = null;
atTable.releaseForks(tablePosition);
}
public int tablePosition() {
return tablePosition;
}
}---------------------------------------------------------
/*
* To change this template, choose Tools | Templates
* <strong class="highlight">and</strong> open the template in the editor.
*/
package diningphilosophers;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
*
*/
public class Table {
private Fork[] forks = new Fork[10];
private Philosopher[] <strong class="highlight">philosophers</strong> = new Philosopher[10];
public Table() {
for(int i = 0; i < forks.length; i++) {
forks[i] = new Fork();
<strong class="highlight">philosophers</strong>[i] = null;
}
}
public boolean sitAt(Philosopher eater) {
int index = eater.tablePosition();
if(index >= 0 && index < <strong class="highlight">philosophers</strong>.length) {
<strong class="highlight">philosophers</strong>[index] = eater;
eater.setTable(this);
return true;
}
return false;
}
public Fork getLeftFork(int index) {
if (index < 0 && index >= forks.length) {
System.out.println("Something went wrong...");
System.exit(1);
}
return forks[index].grabFork(<strong class="highlight">philosophers</strong>[index]);
}
public Fork getRightFork(int index) {
if (index < 0 && index >= forks.length) {
System.out.println("Something went wrong...");
System.exit(1);
}
return forks[(index + 1)%forks.length].grabFork(<strong class="highlight">philosophers</strong>[index]);
}
public void releaseForks(int index) {
forks[index].releaseFork(<strong class="highlight">philosophers</strong>[index]);
forks[(index+1)%forks.length].releaseFork(<strong class="highlight">philosophers</strong>[index]);
}
}
代码块移动到代码周围-OriginalGriff [/edit]
[edit]Code block move to around the code - OriginalGriff[/edit]
推荐答案
这篇关于餐饮哲学家,但十个哲学家.我想更改此解决方案.我想完成这段代码.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!