防止四路路口的汽车在java中崩溃
我制作了一个 4 路连接点的 java 应用程序。我可以使用 THread.sleep() 将所有汽车移过交叉路口,但我需要使汽车不会相互碰撞。 (见图)
我应该使用什么?
同步
wait()/notify()/notifyAll()
ThreadPanel
Canvas(顺便说一句Canvas 是什么及其用途?)
我使用了layeredPane将图像放在彼此之上。
这是我的代码:
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();
}
}
感谢任何帮助。谢谢您的宝贵时间。
非常感谢
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)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以认为有 4 个共享资源需要同步访问:“连接点”的四个角。要过马路,每辆车必须首先进入距离它们最近的拐角,然后进入下一个拐角。
但是,如果单独锁定每个角,则可能会由于 哲学家就餐问题。当所有四辆汽车进入(“锁定”)最近的方格,并且所有四辆汽车都等待下一辆顺时针方向的汽车清除第二个方格时,就会发生这种情况。请参阅该链接以获取解决方案。
将交集视为四种资源当然只是一种解决方案。您还可以考虑将整个交叉路口视为建议的资源(类似于全程停车,但不完全一样),尽管这也不能密切反映实际的交通流量。您还可以为镜像交通信号灯的四个角建立同步规则。
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.
如果您不将其中一条道路指定为“主要”道路,而将另一条道路指定为“次要”。否则即使在现实生活中也无法确定哪辆车应该先走
您可以尝试
CountDownLatch
。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
.