#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    25
    Rep Power
    0

    Concurrent Update Issue


    I have a basic java programming. The question is there are 2 stations who share a same runways, lets make it as station A (SA) and station B (SB). When SA assign an airplane to landing, SB couldn't assign any airplane to landing or departure until SA release it. And once SA landed the runways, it will park on docking station and i assume there is maximum 4 docking station in the airport.
    The problem is when i run 2 threads together, once i allow SA to assign an airplane to the runways at the same time SB also allow to assign an airplane to runways too. And after SA's airplane reach the docking station and update the value, also SB reach the docking station, the docking station doesn't update at all. Can anyone please solve my problem?? Thanks. My code will be show on below:

    Code:
    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();
            
        }
    }
    ____________________________________________________________________________________
    Code:
    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();
            
        }
    }
    __________________________________________________________________________
    Code:
    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;
            }
    
        }
    I hope anyone will tell me the "answer" that what exactly should i do. Thanks.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,534
    Rep Power
    1906
    The problem is that you have the read and assignment in two different call, as shown en the code below.
    This mean both thread can execute/call getCurrentRunway before it is "locked" with setCurrentRunway.
    Code:
                        currentRunway = b.getCurrentRunway();
                            if (currentRunway == 0){
                                b.setCurrentRunway(1);
    You problem looks similar to the producer-consumer issue, which you can read more about in the explanation of Guarded Blocks.
    Especially the part with the variable empty which could correspond to your variable currentRunway.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    25
    Rep Power
    0
    I'm still don't understand what should i do, can you give me the solution by putting the code inside my assignment and explain it?? Thanks.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,534
    Rep Power
    1906
    It will be better if you try to describe which part you don't understand.

    The idea is that the the three lines i quoted will have to be placed/merged into one synchronized function. This will be similar to the function guardedJoy() and take() in the linked example.
    Code:
    public synchronized void getRunway() {
    
        while(RunwayInUse) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
    	RunwayInUse = true;
        System.out.println("Runway is in use");
    }
    This will "pause" the thread until the Runway is free. Instead of wait, you can modify it to return true/false instead.

    To release the Runway, you will have to modified setCurrentRunway to look like notifyJoy
    Code:
    public synchronized releaseRunway() {
        RunwayInUse= false;
        notifyAll(); 
    }
    It should be noted that I haven't tested the code.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    25
    Rep Power
    0
    I'm sorry that i not really good in java that's why i'm asking for edit with my code and implement it for me. Erm.. I'm don't know where should i place the runwayinuse method in and releaserunway too. I'm really appreciate if you could take my code and modify it for me. Thanks.
  10. #6
  11. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,534
    Rep Power
    1906
    in Process, you have a function called getCurrentRunway, this should be like this:
    Code:
    public synchronized int getCurrentRunway() {
    
    	if(this.currentRunway == 0) {
    		this.currentRunway = 1;
    		return 0;
    	}
    	else {
    		System.out.println("Runway is in use");
    		return 1;
    	}
    }
    In rest of the code, you only need to call
    Code:
    b.setCurrentRunway(0);
    to release the Runway

    Other calls can be removed, which will mean places like:
    Code:
    b.setCurrentRunway(1);
    and
    Code:
    currentRunway = 1;
    b.setCurrentRunway(currentRunway);

    Comments on this post

    • darkdevil8z agrees
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    25
    Rep Power
    0
    Thx for the help, I tried to remove the calls and edit the getCurrentRunway(), but somehow when i run the both ATC and ATC2, when ATC try to assign airplane landing at the same time ATC2 also can assign another airplane. Although it still cant works for me but i'm really appreciate your help. You deserve the reputation.
  14. #8
  15. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,534
    Rep Power
    1906
    From what i understand, you have two screens with output, one for ATC and one for ATC2.

    How do you know the order of the running. Can't see you have any timings in the output.
    A lot of "things" can happen in 1 second when talking about computer and threading, which we will recognize as "same time".

IMN logo majestic logo threadwatch logo seochat tools logo