📄 eat.java~43~
字号:
package eat;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* <p>Title: Philosefers Eating Problem</p>
* <p>Description: Operating System_Thread</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author Tse
* @version 1.0
*/
public class eat extends JFrame {
JPanel contentPane;
JButton jButton1 = new JButton();
JTextArea jTextArea1 = new JTextArea();
//
int x1 = 1;
int x2 = 1;
int x3 = 1;
int x4 = 1;
int x5 = 1;
//Construct the frame
public eat() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Eating");
jButton1.setBounds(new Rectangle(150, 117, 81, 37));
jButton1.setFocusPainted(true);
jButton1.setText("进餐");
jButton1.addActionListener(new eat_jButton1_actionAdapter(this));
jTextArea2.setBorder(BorderFactory.createLoweredBevelBorder());
jTextArea2.setText("");
jTextArea2.setBounds(new Rectangle(250, 116, 113, 35));
jTextArea3.setBorder(BorderFactory.createLoweredBevelBorder());
jTextArea3.setText("");
jTextArea3.setBounds(new Rectangle(212, 194, 123, 40));
jTextArea4.setBorder(BorderFactory.createLoweredBevelBorder());
jTextArea4.setText("");
jTextArea4.setBounds(new Rectangle(60, 194, 115, 39));
jTextArea5.setBorder(BorderFactory.createLoweredBevelBorder());
jTextArea5.setText("");
jTextArea5.setBounds(new Rectangle(19, 113, 120, 37));
jTextArea1.setBorder(BorderFactory.createLoweredBevelBorder());
jTextArea1.setBounds(new Rectangle(133, 54, 121, 32));
Eater1.setBorder(BorderFactory.createEtchedBorder());
Eater1.setHorizontalAlignment(SwingConstants.CENTER);
Eater1.setText("哲学家1");
Eater1.setBounds(new Rectangle(134, 32, 118, 16));
jLabel1.setBorder(BorderFactory.createEtchedBorder());
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("哲学家2");
jLabel1.setBounds(new Rectangle(252, 96, 112, 15));
jLabel2.setBorder(BorderFactory.createEtchedBorder());
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setText("哲学家3");
jLabel2.setBounds(new Rectangle(215, 174, 117, 15));
jLabel3.setBorder(BorderFactory.createEtchedBorder());
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setText("哲学家4");
jLabel3.setBounds(new Rectangle(62, 176, 115, 14));
jLabel4.setBorder(BorderFactory.createEtchedBorder());
jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
jLabel4.setText("哲学家5");
jLabel4.setBounds(new Rectangle(19, 95, 120, 15));
contentPane.add(jButton1, null);
contentPane.add(jTextArea3, null);
contentPane.add(jTextArea5, null);
contentPane.add(jTextArea4, null);
contentPane.add(jTextArea2, null);
contentPane.add(jTextArea1, null);
contentPane.add(Eater1, null);
contentPane.add(jLabel1, null);
contentPane.add(jLabel2, null);
contentPane.add(jLabel3, null);
contentPane.add(jLabel4, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public class SyncStack {
boolean l1=true,l2=true,l3=true,l4=true,l5=true;
public synchronized boolean Think(int Left,int Right ) {
while (Left == 0) {
try {
this.wait();
}
catch (InterruptedException e) {}
return false;
}
while (Right == 0) {
try {
this.wait();
}
catch (InterruptedException e) {}
return false;
}
this.notify();
return true;
}
public synchronized void Eat() {
try {
Thread.sleep( 3000 );
}
catch (InterruptedException e) {}
}
public synchronized void Hungry(){
try {
Thread.sleep( (int) (Math.random() * 1000));
}
catch (InterruptedException e) {}
}
}
class Eater1 implements Runnable {
SyncStack stack;
public Eater1(SyncStack s) {
stack = s;
}
public void run() {
while (true) {
jTextArea1.setText("");
jTextArea1.append("哲学家1 is Hunger\n");
stack.Hungry();
jTextArea1.setText("");
jTextArea1.append("哲学家1 is Thinking\n");
try {
Thread.sleep( (int) (Math.random() * 100));
}
catch (InterruptedException e) {}
if(stack.l1&&stack.l5){
stack.l1=false;
stack.l5=false;
if (stack.Think(x1, x5)) {
x1--;
x5--;
jTextArea1.setText("");
jTextArea1.append("哲学家1 is Eating\n");
stack.Eat();
stack.l1=true;
stack.l5=true;
x1++;
x5++;
}
}
}
}
}
class Eater2 implements Runnable {
SyncStack stack;
public Eater2(SyncStack s) {
stack = s;
}
public void run() {
while (true) {
jTextArea2.setText("");
jTextArea2.append("哲学家2 is Hunger\n");
stack.Hungry();
jTextArea2.setText("");
jTextArea2.append("哲学家2 is Thinking\n");
try {
Thread.sleep( (int) (Math.random() * 100));
}
catch (InterruptedException e) {}
if(stack.l1&&stack.l2){
stack.l1=false;
stack.l2=false;
if ( stack.Think(x2, x1)) {
x2--;
x1--;
jTextArea2.setText("");
jTextArea2.append("哲学家2 is Eating\n");
stack.Eat();
stack.l1=true;
stack.l2=true;
x1++;
x2++;
}
}
}
}
}
class Eater3 implements Runnable {
SyncStack stack;
public Eater3(SyncStack s) {
stack = s;
}
public void run() {
while (true) {
jTextArea3.setText("");
jTextArea3.append(" 哲学家3 is Hunger\n");
stack.Hungry();
jTextArea3.setText("");
jTextArea3.append("哲学家3 is Thinking\n");
try {
Thread.sleep( (int) (Math.random() * 100));
}
catch (InterruptedException e) {}
if(stack.l2&&stack.l3){
stack.l3=false;
stack.l2=false;
if (stack.Think(x3, x2)) {
x3--;
x2--;
jTextArea3.setText("");
jTextArea3.append("哲学家3 is Eating\n");
stack.Eat();
stack.l3=true;
stack.l2=true;
x3++;
x2++;
}
}
}
}
}
class Eater4 implements Runnable {
SyncStack stack;
public Eater4(SyncStack s) {
stack = s;
}
public void run() {
while (true) {
jTextArea4.setText("");
jTextArea4.append("哲学家4 is Hunger\n");
stack.Hungry();
jTextArea4.setText("");
jTextArea4.append("哲学家4 is Thinking\n");
try {
Thread.sleep( (int) (Math.random() * 100));
}
catch (InterruptedException e) {}
if(stack.l3&&stack.l4){
stack.l4=false;
stack.l3=false;
if (stack.Think(x4, x3)) {
x4--;
x3--;
jTextArea4.setText("");
jTextArea4.append("哲学家4 is Eating\n");
stack.Eat();
stack.l4=true;
stack.l3=true;
x4++;
x3++;
}
}
}
}
}
class Eater5 implements Runnable {
SyncStack stack;
public Eater5(SyncStack s) {
stack = s;
}
public void run() {
while (true) {
jTextArea5.setText("");
jTextArea5.append("哲学家5 is Hunger\n");
stack.Hungry();
jTextArea5.setText("");
jTextArea5.append("哲学家5 is Thinking\n");
try {
Thread.sleep( (int) (Math.random() * 100));
}
catch (InterruptedException e) {}
if(stack.l4&&stack.l5){
stack.l5=false;
stack.l4=false;
if (stack.Think(x5, x4)) {
x5--;
x1--;
jTextArea5.setText("");
jTextArea5.append("哲学家5 is Eating\n");
stack.Eat();
stack.l4=true;
stack.l5=true;
x1++;
x5++;
}
}
}
}
}
//////////////////
SyncStack stack = new SyncStack();
Runnable p1 = new Eater1(stack);
Thread e1 = new Thread(p1);
Runnable p2 = new Eater2(stack);
Thread e2 = new Thread(p2);
Runnable p3 = new Eater3(stack);
Thread e3 = new Thread(p3);
Runnable p4 = new Eater4(stack);
Thread e4 = new Thread(p4);
Runnable p5 = new Eater5(stack);
Thread e5 = new Thread(p5);
JTextArea jTextArea2 = new JTextArea();
JTextArea jTextArea3 = new JTextArea();
JTextArea jTextArea4 = new JTextArea();
JTextArea jTextArea5 = new JTextArea();
JLabel Eater1 = new JLabel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
void jButton1_actionPerformed(ActionEvent e) {
e1.start();
e2.start();
e3.start();
e4.start();
e5.start();
}
}
class eat_jButton1_actionAdapter implements java.awt.event.ActionListener {
eat adaptee;
eat_jButton1_actionAdapter(eat adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -