gpt4 book ai didi

java - 并发更新问题

转载 作者:行者123 更新时间:2023-12-01 04:41:12 24 4
gpt4 key购买 nike

我有基本的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;
}

}

我希望有人能一步一步告诉我我应该做什么。谢谢。

最佳答案

如何表示跑道限制有多种可能性。

我个人在很多方面都喜欢队列 - 它们似乎符合我的心态。我从未使用过 SynchronousQueue但是,从 JavaDocs 来看,它可能正是您想要的。

您还可以使用ArrayBockingQueue 容量为1。

YMMV。我打赌您会有很多其他选择。

关于java - 并发更新问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16501163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com