早教吧 育儿知识 作业答案 考试题库 百科 知识分享

100分解决javaimportjava.awt.Point;importjava.awt.Rectangle;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;publicclassDiningextendsJFrame{privates

题目详情
100分 解决java
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Dining extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton jButton = null;
private JScrollPane jScrollPane = null;
private JTextArea jTextArea = null;
/**
* This is the default constructor
*/
static Chopsticks[] stick=new Chopsticks[5];
static Philosopher[] philosopher=new Philosopher[5];
static Thread[] thread=new Thread[5];//五个线程代表五个哲学家
public Dining() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(365,361);
this.setLocation(new Point(240,320));
this.setContentPane(getJContentPane());
this.setTitle("哲学家就餐问题");
}
如何设置入口,使程序能运行
public void run(){
while(true){
eat();//吃东西
try{
Thread.sleep(7000);//睡眠7000毫秒
}catch(InterruptedException e){
e.printStackTrace();
}
think();//思考
try{
Thread.sleep(7000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
▼优质解答
答案和解析
加一个main方法噻
public static void main(String args[]){
Dining d=new Dining();
d.setVisible(true);
}
这个小程序貌似在讲解线程的同步和死锁
问题很经典,不过如果用银行转账那个来举例应该更容易理解