我有一个基本的java编程。问题是有 2 个站点共享同一条跑道,我们将其设为 A 站 (SA) 和 B 站 (SB)。当 SA 分配一架飞机着陆时,SB 不能分配任何飞机着陆或起飞,直到 SA 释放它。一旦 SA 降落在跑道上,它将停在停靠站,我假设机场最多有 4 个停靠站。问题是当我一起运行 2 个线程时,一旦我允许 SA 将飞机分配到跑道,同时 SB 也允许将飞机分配到跑道。SA的飞机到达对接站并更新值后,SB也到达对接站,对接站根本不更新。谁能解决我的问题??谢谢。我的代码将显示在下面:
ATC.java
package ccsd;
import java.io.*;
import java.net.*;
public class ATC implements Runnable
{
static Process test = new Process();
Process b;
int option;
int currentRunway;
int currentDockSpace;
int airplaneID;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
ATC (Process a) {
b = a;
}
public void run()
{
try
{
System.out.println("Air Traffic Control");
System.out.println("1. Assign airplane to landing.");
System.out.println("2. Assign airplane to departure.");
System.out.println("Enter the option: ");
option = Integer.parseInt(bufferedReader.readLine());
if (option == 1){
currentRunway = b.getCurrentRunway();
currentDockSpace = b.getDockSpace();
if (currentRunway == 0 && currentDockSpace < 5){
currentRunway = 1;
b.setCurrentRunway(currentRunway);
System.out.println("Input airplane id: ");
airplaneID = Integer.parseInt(bufferedReader.readLine());
b.setAirplaneID(airplaneID);
System.out.println("Currently airplane "+ b.getAirplaneID() + "is going land.");
System.out.println("Processing in... 10.. \n9..\n8..\n7..\n6..\n5..\n4..\n3..\n2..\n1..");
System.out.println("Airplane" + b.getAirplaneID() + " had been landed.");
b.dockIn();
b.setCurrentRunway(0);
System.out.println("Currently the run way is clear now and "+ b.getDockSpace() + " docking space used.");
}
else
System.out.println("Currently there is an airplane landing or lack docking space, please try again later.");
run();
}
else if (option == 2){
currentDockSpace = b.getDockSpace();
if (currentDockSpace>0){
System.out.println("Input airplane id: ");
airplaneID = Integer.parseInt(bufferedReader.readLine());
b.setAirplaneID(airplaneID);
currentRunway = b.getCurrentRunway();
if (currentRunway == 0){
b.setCurrentRunway(1);
System.out.println("Currently airplane "+ b.getAirplaneID() + "is departuring.");
System.out.println("Processing in... 10.. \n9..\n8..\n7..\n6..\n5..\n4..\n3..\n2..\n1..");
System.out.println("Airplane" + b.getAirplaneID() + " had been departured.");
b.dockOut();
b.setCurrentRunway(0);
System.out.println("Currently the run way is clear now and "+ b.getDockSpace() + " docking space used.");
}
else
{
System.out.println("Currently there is an airplane landing, please try again later.");
run();
}
}
else
{
System.out.println("Currently there is no any airplane in the docking, please try again later.");
run();
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException{
Thread thread1 = new Thread(new ATC(test));
thread1.start();
}
}
ATC2.java
package ccsd;
import java.io.*;
import java.net.*;
public class ATC2 implements Runnable
{
Process b;
int option;
int currentRunway;
int currentDockSpace;
int airplaneID;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
ATC2 (Process a) {
b = a;
}
public void run()
{
try
{
System.out.println("Air Traffic Control");
System.out.println("1. Assign airplane to landing.");
System.out.println("2. Assign airplane to departure.");
System.out.println("Enter the option: ");
option = Integer.parseInt(bufferedReader.readLine());
if (option == 1){
currentRunway = b.getCurrentRunway();
currentDockSpace = b.getDockSpace();
if (currentRunway == 0 && currentDockSpace < 5){
b.setCurrentRunway(1);
System.out.println("Input airplane id: ");
airplaneID = Integer.parseInt(bufferedReader.readLine());
b.setAirplaneID(airplaneID);
System.out.println("Currently airplane "+ b.getAirplaneID() + "is going land.");
System.out.println("Processing in... 10.. \n9..\n8..\n7..\n6..\n5..\n4..\n3..\n2..\n1..");
System.out.println("Airplane" + b.getAirplaneID() + " had been landed.");
b.dockIn();
b.setCurrentRunway(0);
System.out.println("Currently the run way is clear now and "+ b.getDockSpace() + " docking space used.");
}
else
System.out.println("Currently there is an airplane landing or lack docking space, please try again later.");
run();
}
else if (option == 2){
currentDockSpace = b.getDockSpace();
if (currentDockSpace>0){
System.out.println("Input airplane id: ");
airplaneID = Integer.parseInt(bufferedReader.readLine());
b.setAirplaneID(airplaneID);
currentRunway = b.getCurrentRunway();
if (currentRunway == 0){
b.setCurrentRunway(1);
System.out.println("Currently airplane "+ b.getAirplaneID() + "is departuring.");
System.out.println("Processing in... 10.. \n9..\n8..\n7..\n6..\n5..\n4..\n3..\n2..\n1..");
System.out.println("Airplane" + b.getAirplaneID() + " had been departured.");
b.dockOut();
b.setCurrentRunway(0);
System.out.println("Currently the run way is clear now and "+ b.getDockSpace() + " docking space used.");
}
else
{
System.out.println("Currently there is an airplane landing, please try again later.");
run();
}
}
else
{
System.out.println("Currently there is no any airplane in the docking, please try again later.");
run();
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException{
Thread thread2 = new Thread(new ATC2(ATC.test));
thread2.start();
}
}
Process.java
package ccsd;
public class Process {
int airplaneID;
int currentRunway;
int dock;
public synchronized void setAirplaneID(int airplaneID)
{
this.airplaneID = airplaneID;
}
public int getAirplaneID()
{
return airplaneID;
}
public synchronized void setCurrentRunway(int currentRunway)
{
this.currentRunway = currentRunway;
}
public int getCurrentRunway()
{
return currentRunway;
}
public synchronized void dockIn()
{
dock++;
}
public synchronized void dockOut()
{
dock--;
}
public int getDockSpace()
{
return dock;
}
}
我希望有人会一步一步地告诉我我该怎么做。谢谢。