私は基本的な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;
}
}
誰かが私に何をすべきかを段階的に教えてくれることを願っています。ありがとう。