我有一个基本的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;
}
}
我希望有人能一步一步告诉我该怎么做。感谢。
答案 0 :(得分:0)
如何表示跑道限制有很多种可能性。
我个人喜欢排队很多东西 - 它们似乎符合我的心态。我从来没有使用过SynchronousQueue,但是从JavaDocs中,它可能就是你想要的。
您还可以使用容量为1的ArrayBockingQueue 。
YMMV。我打赌你会得到很多其他的选择。
答案 1 :(得分:0)
您必须通过多个线程进行同步。最好的办法是为每条跑道使用计数信号量(java.util.concurrent.Semaphore)。也许甚至对于每个扩展坞也是如此。使用这种技术,您可以根据需要将任意数量的对象与任意数量的进程(线程)同步。