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

    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 offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    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 03:57 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    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
    Zurich, Switzerland
    Rep Power


    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) {
    	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 {
    					} catch (InterruptedException e) {
    					System.out.println(this.getRunway() + ": " + f.getName() + " landed.");

    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");
    		// 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");
    		// start your airport simulation and run it for 6 seconds
    		try {
    		} catch (InterruptedException e) {
    		// interrrupt the threads, because after 6 seconds all flights 
    		// should be landeded

    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 offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    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,


Similar Threads

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