⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yinhangjia.java

📁 c.rar___z8786052200751110333362469.rar
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
						jLabel3_2_1.setText("It is not safe:");
					}
				}
			});
		}
		return safecheckMenuItem;
	}

	private JMenuItem getRequesMenuItem() {
		if (requesMenuItem == null) {
			requesMenuItem = new JMenuItem();
			requesMenuItem.setText("资源请求");
			requesMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					jPanel3.setVisible(false);
					shuRuGP1_3();
				}
			});
		}
		return requesMenuItem;
	}

	/**
	 * Launches this application
	 */
	public void actionPerformed(ActionEvent e) {
		L1:if (e.getActionCommand() == "提交Max") {
			int k = 0;
			String str="";
			for (int i = 1; i <= M; i++) {
				if ((jLabel1_2_1_1.getText()).equals("请输入Max(" + i + ")")) {
					k = i;
					break;
				}
			}
			str=jLabel1_2_1_1.getText();
		 for (int j = 0; j <= N - 1; j++) {
				if(!(textfield1[j].getText().equals("")))
				{
				max[k - 1][j] = getInt(textfield1[j].getText());
				textfield1[j].setText("");
				}
				else
				{
					JOptionPane.showMessageDialog(jContentPane,"不能为空,请重新输入","输入错误",JOptionPane.INFORMATION_MESSAGE);
					textfield1[j].setText("");
					jLabel1_2_1_1.setText(str);
					jButton1_2_3_1.setText("提交Max");
					break L1;
				}
			}
			k = k + 1;
			if (k <= M) {
				jLabel1_2_1_1.setText("请输入Max(" + k + ")");
			}
			if (k > M) {
				jLabel1_2_1_1.setText("请输入Allocation(1)");
				jButton1_2_3_1.setText("提交Allocation");
			}
		}

	 L2:if (e.getActionCommand() == "提交Allocation") {
			int k = 0;
			String str="";
			for (int i = 1; i <= M; i++) {
				if ((jLabel1_2_1_1.getText())
						.equals("请输入Allocation(" + i + ")")) {
					k = i;
					break;
				}
			}
			str=jLabel1_2_1_1.getText();
			for (int j = 0; j <= N - 1; j++) {
				if(!(textfield1[j].getText().equals("")))
				{
				allocation[k - 1][j] = getInt(textfield1[j].getText());
				textfield1[j].setText("");
				}
				else
				{
					JOptionPane.showMessageDialog(jContentPane,"不能为空,请重新输入","输入错误",JOptionPane.INFORMATION_MESSAGE);
					textfield1[j].setText("");
					jLabel1_2_1_1.setText(str);
					jButton1_2_3_1.setText("提交Allocation");
					break L2;
				}
			}
			k = k + 1;
			if (k <= M) {
				jLabel1_2_1_1.setText("请输入Allocation(" + k + ")");
			}
			if (k > M) {
				jLabel1_2_1_1.setText("请输入系统总资源");
				jButton1_2_3_1.setText("提交Source");

			}
		}
	  L3:if (e.getActionCommand() == "提交Source") {
			String str="";
			str=jLabel1_2_1_1.getText();
			for (int i = 0; i < textfield1.length; i++) {
				for (int j = 0; j <= N - 1; j++) {
					if(!(textfield1[j].getText().equals("")))
					{
					source[j] = getInt(textfield1[j].getText());
					}
					else
					{
						JOptionPane.showMessageDialog(jContentPane,"不能为空,请重新输入","输入错误",JOptionPane.INFORMATION_MESSAGE);
						textfield1[j].setText("");
						jLabel1_2_1_1.setText(str);
						jButton1_2_3_1.setText("提交Source");
						break L3;
					}
				}

			}

			shuZuJiSuan();
			if (jianChaF()) {
				if (jianChaS())
					if (jianChaT()) {
						jPanel1_2.setVisible(false);
						shuRuJDesktopP2();
						getControlMenu().setEnabled(true);
					} else {
						shuRuGP1_2();
					}
				else {
					shuRuGP1_2();
				}

			} else {
				shuRuGP1_2();
			}
		}
	}

	public void shuZuJiSuan() {
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < N; j++) {
				need[i][j] = max[i][j] - allocation[i][j];
			}
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				sum[i] = sum[i] + allocation[j][i];
			}
		}
		for (int i = 0; i < N; i++) {
			work[i] = source[i] - sum[i];
		}
	}

	public void chuShiShuZu() {
		max = new int[M][N];
		allocation = new int[M][N];
		need = new int[M][N];
		source = new int[N];
		work = new int[N];
		sum = new int[N];
		request = new int[N];
	}

	public void shuRuJDesktopP2() {

		JLabel jlabel1, jlabel2, jlabel3, jlabel4, jlabel5;
		JLabel[][] jlabel = new JLabel[M][5];
		jlabel1 = new JLabel("进程\\资源");
		jlabel2 = new JLabel("Max()");
		jlabel3 = new JLabel("Allocation()");
		jlabel4 = new JLabel("Need");
		jlabel5 = new JLabel("Available");
		jlabel1.setBounds(40, 12, 75, 25);
		jlabel1.setBorder(javax.swing.BorderFactory.createLineBorder(
				java.awt.Color.blue, 1));
		jDesktopPane2.add(jlabel1);
		jlabel2.setBounds(115, 12, 75, 25);
		jlabel2.setBorder(javax.swing.BorderFactory.createLineBorder(
				java.awt.Color.blue, 1));
		jDesktopPane2.add(jlabel2);
		jlabel3.setBounds(190, 12, 75, 25);
		jlabel3.setBorder(javax.swing.BorderFactory.createLineBorder(
				java.awt.Color.blue, 1));
		jDesktopPane2.add(jlabel3);
		jlabel4.setBounds(265, 12, 75, 25);
		jlabel4.setBorder(javax.swing.BorderFactory.createLineBorder(
				java.awt.Color.blue, 1));
		jDesktopPane2.add(jlabel4);
		jlabel5.setBounds(340, 12, 75, 25);
		jlabel5.setBorder(javax.swing.BorderFactory.createLineBorder(
				java.awt.Color.blue, 1));
		jDesktopPane2.add(jlabel5);
		for (int i = 0; i < M; i++) {
			JLabel jlb = new JLabel();
			int y;
			y = 37 + i * 25;
			jlb.setBounds(40, y, 75, 25);
			jlb.setBorder(javax.swing.BorderFactory.createLineBorder(
					java.awt.Color.blue, 1));
			jlb.setText("P" + i);
			jDesktopPane2.add(jlb);
		}
		for (int i = 0; i < M; i++) {
			String str1 = "";
			for (int j = 0; j < N; j++) {
				str1 = str1 + max[i][j] + " ";
			}
			int y;
			y = 37 + i * 25;
			jlabel[i][1] = new JLabel();
			jlabel[i][1].setBounds(115, y, 75, 25);
			jlabel[i][1].setBorder(javax.swing.BorderFactory.createLineBorder(
					java.awt.Color.blue, 1));
			jlabel[i][1].setText(str1);
			jDesktopPane2.add(jlabel[i][1]);
		}
		for (int i = 0; i < M; i++) {
			String str1 = "";
			for (int j = 0; j < N; j++) {
				str1 = str1 + allocation[i][j] + " ";
			}
			int y;
			y = 37 + i * 25;
			jlabel[i][2] = new JLabel();
			jlabel[i][2].setBounds(190, y, 75, 25);
			jlabel[i][2].setBorder(javax.swing.BorderFactory.createLineBorder(
					java.awt.Color.blue, 1));
			jlabel[i][2].setText(str1);
			jDesktopPane2.add(jlabel[i][2]);

		}
		for (int i = 0; i < M; i++) {
			String str1 = "";
			for (int j = 0; j < N; j++) {
				str1 = str1 + need[i][j] + " ";
			}
			int y;
			y = 37 + i * 25;
			jlabel[i][3] = new JLabel();
			jlabel[i][3].setBounds(265, y, 75, 25);
			jlabel[i][3].setBorder(javax.swing.BorderFactory.createLineBorder(
					java.awt.Color.blue, 1));
			jlabel[i][3].setText(str1);
			jDesktopPane2.add(jlabel[i][3]);

		}
		for (int i = 0; i < M; i++) {
			String str1 = "";
			for (int j = 0; j < N; j++) {
				str1 = str1 + work[j] + " ";
			}
			int y;
			y = 37 + i * 25;
			jlabel[i][4] = new JLabel();
			jlabel[i][4].setBounds(340, y, 75, 25);
			jlabel[i][4].setBorder(javax.swing.BorderFactory.createLineBorder(
					java.awt.Color.blue, 1));
			jlabel[i][4].setText(str1);
			jDesktopPane2.add(jlabel[i][4]);
			if (i == 0) {
				jlabel[i][4].setText(str1);
			} else
				jlabel[i][4].setText("");
		}

	}

	public void shuRuGP3() {
		jPanel3.setVisible(true);
		jLabel3_1_1.setText("");
		jLabel3_2_1.setText("");
		jButton1_3_3_1.setText("确定");
	}

	public void shuRuGP1_3() {
		jPanel1_1.setVisible(false);
		jPanel1_2.setVisible(false);
		jPanel1_3.setVisible(true);
		jPanel1_3_2.removeAll();
		jPanel1_3_2.repaint();
		jLabel1_3_1_1.setText("Request");
		jButton1_3_3_1.setText("提交Request");
		jTextField1_3_1_1.setText("");
		textfield2 = new JTextField[N];
		for (int i = 0; i < textfield2.length; i++) {
			textfield2[i] = new JTextField(4);
			textfield2[i].setToolTipText("不能为空");
			textfield2[i].addKeyListener(this);
			jPanel1_3_2.add(textfield2[i]);
		}
	}

	public void shuRuGP1_2() {
		jPanel1_1.setVisible(false);
		jPanel1_2.setVisible(true);
		jPanel1_3.setVisible(false);
		jPanel1_2_2.removeAll();
		jPanel1_2_2.repaint();
		jLabel1_2_1_1.setText("请输入Max(1)");
		jButton1_2_3_1.setText("提交Max");
		textfield1 = new JTextField[N];
		for (int i = 0; i < textfield1.length; i++) {
			textfield1[i] = new JTextField(4);
			textfield1[i].setToolTipText("不能为空");
			textfield1[i].addKeyListener(this);
			jPanel1_2_2.add(textfield1[i], null);
		}
	}

	public void shuRuGP1_1() {
		jPanel1_1.setVisible(true);
		jPanel1_2.setVisible(false);
		jPanel1_3.setVisible(false);
		jLabel1_1_1_1.setText("共有几个进程:");
		jLabel1_1_2_1.setText("共有几类资源:");
		jTextField1_1_1_1.setText("");
		jTextField1_1_2_1.setText("");
		jButton1_1_3_1.setText("确定");
	}

	public int getInt(String str) {
		if (str.equals(""))
			return 0;
		else
			return Integer.parseInt(str);
	}

	public boolean safeCheck() {
		if (jianChaF())
			if (jianChaS())
				if (jianChaT()) {
					int[][] checkmax = new int[M][N];
					int[][] checkallocation = new int[M][N];
					int[][] checkneed = new int[M][N];
					int[] checkwork = new int[N];
					int[] record = new int[M];
					int count = 0;
					s = "";
					boolean[][] p = new boolean[M][2];
					for (int i = 0; i < M; i++)
						for (int j = 0; j < N; j++) {
							checkmax[i][j] = max[i][j];
							checkallocation[i][j] = allocation[i][j];
							checkneed[i][j] = need[i][j];
						}
					for (int i = 0; i < N; i++)
						checkwork[i] = work[i];
					for (int i = 0; i < M; i++) {
						for (int j = 0; j < M; j++) {
							if (p[j][0]) {
								if (!p[j][1]) {
									record[count] = j;
									p[j][1] = true;
									count++;
								}
							} else {
								int l = 0;
								for (int k = 0; k < N; k++) {
									if (checkneed[j][k] <= checkwork[k]) {
										l++;
									}

								}
								if (l == N) {
									p[j][0] = true;
									p[j][1] = true;
									record[count] = j;
									count++;
									for (int k = 0; k < N; k++) {
										checkwork[k] = checkwork[k]
												+ checkallocation[j][k];
									}
								}

							}
						}
					}
					if (count >= M) {
						for (int i = 0; i < M; i++) {
							s = s + "P" + String.valueOf(record[i]) + "->";
						}
						return true;
					} else
						s = "";

				}
		return false;

	}

	public boolean jianChaF() {
		int a = 0, b = 0;
		l: for (a = 0; a < M; a++) {
			for (b = 0; b < N; b++)
				if (max[a][b] > source[b]) {
					b++;
					JOptionPane.showMessageDialog(jContentPane, "P" + a + "第"
							+ b + "类资源需求超过系统总资源请重新输入", "输入错误",
							JOptionPane.INFORMATION_MESSAGE);
					break l;
				}
		}
		if (a == M && b == N)
			return true;
		else
			return false;
	}

	public boolean jianChaS() {
		int a = 0, b = 0;
		l: for (a = 0; a < M; a++) {
			for (b = 0; b < N; b++) {
				if (allocation[a][b] > max[a][b]) {
					b++;
					JOptionPane.showMessageDialog(jContentPane, "P" + a
							+ "类资源的Allocation()>Max()请重新输入", "输入错误",
							JOptionPane.INFORMATION_MESSAGE);
					break l;
				}
			}
		}
		if (a == M && b == N)
			return true;
		else
			return false;
	}

	public boolean jianChaT() {
		int a = 0, b = 0;
		l: for (a = 0; a < M; a++) {
			for (b = 0; b < N; b++) {
				if (need[a][b] > max[a][b]) {
					b++;
					JOptionPane.showMessageDialog(jContentPane, "P" + a
							+ "类资源的Need()>Max()请重新输入", "输入错误",
							JOptionPane.INFORMATION_MESSAGE);
					break l;
				}
			}
		}
		if (a == M && b == N)
			return true;
		else
			return false;
	}

	public void requestSource() {
		L = getInt(jTextField1_3_1_1.getText());
		int[] workb = new int[N];
		int[] needb = new int[N];
		int[] allocationb = new int[N];
		int i;
		for (int k = 0; k < N; k++) {
			workb[k] = work[k];
			needb[k] = need[L][k];
			allocationb[k] = allocation[L][k];
		}
		for (int b = 0; b < request.length; b++) {
			request[b] = getInt(textfield2[b].getText());
		}
		for (i = 0; i < N; i++) {
			if ((request[i]) > need[L][i]) {

				JOptionPane.showMessageDialog(jContentPane,
						"所需资源超过其所宣布资源,请重新输入", "Information",
						JOptionPane.INFORMATION_MESSAGE);
				shuRuGP1_3();
				break;
			}

			if (request[i] > work[i]) {
				JOptionPane.showMessageDialog(jContentPane, "尚无资源.让P" + L
						+ "等待", "Information", JOptionPane.INFORMATION_MESSAGE);

				break;
			}
		}
		if (i >= N) {
			for (int j = 0; j < work.length; j++) {
				work[j] = work[j] - request[j];
				allocation[L][j] = allocation[L][j] + request[j];
				need[L][j] = need[L][j] - request[j];
			}
			if (safeCheck()) {
				shuRuGP3();
				jLabel3_1_1.setText(s2 + s);
				jLabel3_2_1.setText("It is safe:进行资源分配");
			} else {
				for (int j = 0; j < workb.length; j++) {
					work[j] = workb[j];
					allocation[L][j] = allocationb[j];
					need[L][j] = needb[j];
				}
				shuRuGP3();
				jLabel3_2_1.setText("It is not safe:不分配资源");

			}

		}
	}

	private JMenuItem getFlashMenuItem() {
		if (flashMenuItem == null) {
			flashMenuItem = new JMenuItem();
			flashMenuItem.setText("刷新");
			flashMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					thread f = new thread();
					f.start();

				}
			});
		}
		return flashMenuItem;
	}

	public class thread extends Thread {
		public void run() {
			jDesktopPane2.removeAll();
			jDesktopPane2.repaint();
			try {
				this.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			shuRuJDesktopP2();
		}
	}

	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("退出");
			exitMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	public static void main(String[] args) {
		YinHangJia application = new YinHangJia();
		application.show();
	}

	public void keyPressed(KeyEvent e) {
		if (e.getKeyChar() < KeyEvent.VK_0 || e.getKeyChar() > KeyEvent.VK_9)
			if (e.getKeyChar() != KeyEvent.VK_BACK_SPACE
					&& e.getKeyChar() != KeyEvent.VK_ENTER&&e.getKeyChar()!=KeyEvent.VK_DELETE) {
				JOptionPane.showMessageDialog(jContentPane, "请输入0-9之间的数",
						"输入错误", JOptionPane.INFORMATION_MESSAGE);
			}

	}

	public void keyReleased(KeyEvent e) {
	}

	public void keyTyped(KeyEvent e) {
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -