要求采用GUI图形化界面模拟多个人通过独木桥的模拟。这个独木桥南北走向,只能容纳一个人过桥,现在桥的两侧分别有9人和8人,编写一个多线程程序,让这些人都能够安全地到达桥的对岸,要求每个人用一个线程表示,桥为共享资源,在过桥的过程中显示谁在过桥,且显示走向显示一下每次通过独木桥人的姓名。
- 用多线程技术实现多人过独木桥;
- 模拟不同速度过桥,速度可自行调整;
- 用面向对象方法设计程序。
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; class Bridge { boolean Enable = false; synchronized void OpenBridge (String thisperson, String Direct, OneWoodBridge oneWoodBridge) throws InterruptedException { if (Enable) { Thread.sleep(100 * oneWoodBridge.time); notify(); oneWoodBridge.directiontext.setText(Direct); oneWoodBridge.thispeopletext.setText(thisperson); System.out.println(thisperson + " " + Direct + " " + oneWoodBridge.time); } } synchronized void CloseBridge () throws InterruptedException { if (!Enable) { wait(); } } } class Person implements Runnable { static int SouthernPerson = 9; static int NorthernPerson = 8; static String[] direct = {"South to North", "North to South"}; static String[] name = {"xv", "lin", "zhang", "gu", "wu", "shui", "huang", "yuan", "liu", "lu", "zheng", "li", "luo", "fang", "hong", "he", "yang"}; String direction; String thisperson; Bridge bridge; OneWoodBridge oneWoodBridge; static int GetPersonOfSouthern () { return SouthernPerson; } static int GetPersonOfNorthern () { return NorthernPerson; } static int SumOfPerson () { return SouthernPerson + NorthernPerson; } static String Direct (int flag) { if (flag == 0) return direct[0]; else return direct[1]; } static String Name (int counter) { return name[counter]; } Person (int flag, int num, Bridge bridge, OneWoodBridge oneWoodBridge) { this.direction = Direct(flag); this.thisperson = Name(num); this.bridge = bridge; this.oneWoodBridge = oneWoodBridge; } @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if (bridge.Enable) { try { bridge.OpenBridge(thisperson, direction, oneWoodBridge); } catch (InterruptedException e) { e.printStackTrace(); } } else { try { bridge.CloseBridge(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class OneWoodBridge extends Jframe implements ActionListener { static OneWoodBridge oneWoodBridge; int time = 10; JTextField directiontext = new JTextField(12); JTextField thispeopletext = new JTextField(12); public OneWoodBridge () { setTitle("OneWoodBridge"); Container container = this.getContentPane(); JPanel jPanel = new JPanel(); JPanel panelsouth = new JPanel(); JButton buttonaction = new JButton("Action"); JButton buttonfast = new JButton("Fast"); JButton button = new JButton("Normal"); JButton buttonslow = new JButton("Slow"); JLabel labeldirect = new JLabel("Direct"); JLabel labelthisperson = new JLabel("This"); JLabel jLabel = new JLabel("South < > North"); JLabel whitespace = new JLabel(" SpeedChoice"); jPanel.add(labeldirect); jPanel.add(directiontext); jPanel.add(labelthisperson); jPanel.add(thispeopletext); jPanel.add(jLabel); panelsouth.add(buttonaction); panelsouth.add(whitespace); panelsouth.add(buttonfast); panelsouth.add(button); panelsouth.add(buttonslow); container.add(jPanel, BorderLayout.NORTH); container.add(panelsouth,BorderLayout.SOUTH); buttonaction.addActionListener(this); buttonfast.addActionListener(new fastlistener()); button.addActionListener(new Normallistener()); buttonslow.addActionListener(new slowlistener()); directiontext.setEditable(false); thispeopletext.setEditable(false); } @Override public void actionPerformed(ActionEvent e) { int Sum = Person.SumOfPerson(); Bridge bridge = new Bridge(); for (int i = 0; i < Person.GetPersonOfSouthern(); i++) { Thread thread = new Thread(new Person(0, i, bridge, oneWoodBridge)); thread.start(); } for (int i = Person.GetPersonOfNorthern(); i > 0 ; i--) { Thread thread = new Thread(new Person(1,Sum - i, bridge, oneWoodBridge)); thread.start(); } bridge.Enable = true; } private class fastlistener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { time = 5; } } private class Normallistener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { time = 10; } } private class slowlistener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { time = 30; } } public static void main(String[] args) { OneWoodBridge oneWoodBridge = new OneWoodBridge(); OneWoodBridge.oneWoodBridge = oneWoodBridge; oneWoodBridge.setSize(600,300); oneWoodBridge.setVisible(true); oneWoodBridge.setResizable(false); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)