Results 1 to 5 of 5
  1. #1
    btap0644 is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Smile How to make two planes land in two different synchronized runways in java ?

    Dear all,

    I posted the question on stackoverflow since it is much clearer over there.

    If you can, kindly refer to it.

    Many Thanks:)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,443
    Rep Power
    25

    Default

    Sounds like a logic problem.
    How have you tried to debug it?

    What's supposed to happen when you execute it?
    Here's what I see in the console:
    Running: java.exe -classpath D:\JavaDevelopment;. -Xmx512M AT_Control

    Plane [111] has arrived at the airport.

    Array Size is : 1
    One thread made
    Flight 111 is about to Land
    Plane [222] has arrived at the airport.

    Array Size is : 2
    One thread made
    Flight 222 is about to Land

    0 error(s)


    Some debugging ideas:
    To track entry and exit to a method(say xyz()):

    before all calls to xyz(), add: System.out.println("calling xyz");
    In xyz() at the beginning: add SOP("entered xyz");
    and just before exiting: add SOP("exiting xyz");

    If you want to know where a method is called from add this to the method:
    try{throw new Exception("who called"?");}catch(Exception x){x.printStackTrace();};
    Last edited by Norm; 07-13-2010 at 02:57 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    That is one heck of a lot of code that you're asking volunteers to go through. I suggest you extract the problem from the GUI, thereby simplifying it greatly and work on the problem in isolation. If you still are having a problem, then post that small simplified code compilable code. Also, if you invite discussion here or elsewhere, good etiquette suggests that you should notify all threads of the cross-posts with links.

    I wish you lots of luck.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    i wrote a small simulation with flights and runways. first the class of runways


    Java Code:
    import java.util.PriorityQueue;
    import java.util.Queue;
    
    public class Runway implements Runnable {
    
    	Queue<Flights> q;
    	String name;
    	private Object syncObject = new Object();
    
    	public Runway(String n) {
    		q = new PriorityQueue<Flights>();
    		name = n;
    		System.out.println(n + " ready.");
    	}
    
    	public void add(Flights f) {
    		q.add(f);
    	}
    
    	public String getRunway() {
    		return name;
    	}
    
    	public void run() {
    		synchronized (syncObject) {
    			while (!Thread.currentThread().isInterrupted()) {
    				if (q.size() > 0) {
    					Flights f = q.peek();
    					System.out.println(this.getRunway() + ": " + f.getName()
    							+ " is about to land");
    					try {
    						Thread.currentThread();
    						Thread.sleep(1000);
    					} catch (InterruptedException e) {
    						Thread.currentThread().interrupt();
    					}
    					System.out.println(this.getRunway() + ": " + f.getName() + " landed.");
    					q.remove(f);
    				}
    			}
    		}
    	}
    }

    and the Flights class


    Java Code:
    public class Flights implements Comparable<Flights> {
    
    	String fName;
    	
    	public Flights(String n) {
    		fName = n;
    	}
    	
    	public String getName() {
    		return fName;
    	}
    	
    	public int compareTo(Flights f) {
    		return this.getName().compareTo(f.getName());
    	}
    	
    	public static void main(String[] args) {
    		// create your Runways
    		Runway r1 = new Runway("Runway 1");
    		Runway r2 = new Runway("Runway 2");
    		
    		// create your Threads
    		Thread t1 = new Thread(r1);
    		Thread t2 = new Thread(r2);
    		
    		// create your Flights and pass it to Runway 1
    		Flights f1 = new Flights("Flight 1");
    		Flights f2 = new Flights("Flight 2");
    		r1.add(f1);
    		r1.add(f2);		
    
    		// create more Flights and pass it to Runway 2
    		Flights f3 = new Flights("Flight 3");
    		Flights f4 = new Flights("Flight 4");
    		Flights f5 = new Flights("Flight 5");
    		r2.add(f3);
    		r2.add(f4);
    		r2.add(f5);
    		
    		// start your airport simulation and run it for 6 seconds
    		t2.start();
    		t1.start();
    		try {
    			Thread.currentThread().sleep(6000);
    		} catch (InterruptedException e) {
    		}
    		
    		// interrrupt the threads, because after 6 seconds all flights 
    		// should be landeded
    		
    		t1.interrupt();
    		t2.interrupt();
    	}
    
    }

    the runway 1 and 2 are running simulaniously. a new flight can enter the landing queue only if the previous flight has landed. hope you got the idea ... when i run the code above i got this output

    Runway 1 ready.
    Runway 2 ready.
    Runway 2: Flight 3 is about to land
    Runway 1: Flight 1 is about to land
    Runway 2: Flight 3 landed.
    Runway 2: Flight 4 is about to land
    Runway 1: Flight 1 landed.
    Runway 1: Flight 2 is about to land
    Runway 1: Flight 2 landed.
    Runway 2: Flight 4 landed.
    Runway 2: Flight 5 is about to land
    Runway 2: Flight 5 landed.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Why isn't anybody using the java.util.concurrent package? It has classes and interfaces that are made for this purpose. A runway can only accomodate one single plane at the time. A plane is put into the runway and the runway stays blocked until the plane is removed from the runway again; in the mean time all other planes that want to use that runway have to wait.

    The java.util.concurrent package has exactly the class for this purpose: the SynchronousQueue; it takes away all the nasty locking and synchronization and all you have to do is put planes in it and remove them again. The class takes care of the thread handling, greatly reducing the size of your own code.

    kind regards,

    Jos

Similar Threads

  1. Synchronized(this)?
    By kiza in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-07-2009, 01:20 PM
  2. Synchronized Servlet
    By udayadas in forum Java Servlet
    Replies: 5
    Last Post: 09-09-2008, 08:14 PM
  3. synchronized
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-28-2007, 10:33 AM
  4. doubt about synchronized
    By simon in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 03:49 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •