views:

127

answers:

2

Hi,

I have made a java application of a 4 way junction. I can to move all the cars across the junction using THread.sleep() but I need to make the cars not crash into one another. (See diagram)

alt text

What should I use ?

  • Synchronization

  • wait()/notify()/notifyAll()

  • ThreadPanels

  • Canvas (btw what is Canvas and its purpose ?)

I have used layeredPane for putting the images on top of each other.

Here is my code:

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JLayeredPane;

import javax.swing.JPanel;

public class Gui {

private JFrame f = new JFrame("Traffic Light");
private JLayeredPane lp = new JLayeredPane();
private JPanel red = new JPanel();
private JPanel car_1 = new JPanel();
private ImageIcon northcar = new ImageIcon("src/north.gif");
private ImageIcon usIcon = new ImageIcon("src/trafficLight.jpg");
private JLabel lb = new JLabel(usIcon);
private JLabel lbcar_1 = new JLabel(northcar);


/*private ImageIcon southcar = new ImageIcon("src/trafficLight.jpg");
private ImageIcon westcar = new ImageIcon("src/trafficLight.jpg");
private ImageIcon eastcar = new ImageIcon("src/trafficLight.jpg");
 */
public Gui() {

    f.setBounds(0, 0, 655, 679);
    f.add(lp);




    car_1.setOpaque(false);
    car_1.setBounds(340, 120, 70, 105);
    //car_1.setBackground(Color.black);
    car_1.add(lbcar_1);




    red.setBounds(0, -5, 650, 650);
    red.add(lb);



    lp.add(red, new Integer(0));
    lp.add(car_1, new Integer(1));


    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    try {
    for (int i = 120; i < 540; i +=1){
            Thread.sleep(10);
            car_1.setBounds(340, i, 70, 105);

        } }catch (Exception e) {
        }


    }

   public static void main(String[] args) {


    Gui frame = new Gui();






   }
   }

Any help is appreciated. Thank you for your time.

Many Thanks

+3  A: 

You can think of there being 4 shared resources that you need to synchronize access to: the four corners of the "junction". To cross the street, each car must first enter the corner closest to them, then the next corner.

However, if you lock each corner separately, you can get into deadlock (the concurrency equivalent of gridlock in your example) due to the Dining Philosopher's Problem. This happens when all four cars enter ("lock") the closest square, and all of them wait on the next clockwise car to clear the second square. See that link for solutions.

Considering the intersection as four resources is of course only one solution. You could also consider treating the entire intersection as a resource as has been suggested (similar to an all-way stop, but not exactly), though that doesn't closely mirror actual traffic flow either. You could also establish synchronization rules for the four corners that mirror a traffic light.

Mark Peters
And just for kicks... [JCIP: Java Concurrency in Practice](http://www.javaconcurrencyinpractice.com/)
pst
Another way to think of this is to think of the lanes as the resources that need the lock for the intersection. There are two roads (north-south, NS, and east-west, EW) and each road has two lanes (NS-north, NS-south, EW-east, EW-west).If it's a four way stop than only one lane can get the lock at any time. If there are no turns allowed than the road gets the lock (i.e. NS is blocked while EW has the lock and all cars going east or west get to move). If turns are allowed than you can use the same idea except right-turn light is a four-way stop while a left-turn light can let all lanes go.
Thien
+1  A: 

If you don't assign one of the roads to be "main", and the other - secondary. Otherwise even in real life there is no way to determine which car should go first

You can try CountDownLatch.

Bozho