I have applied a Java application of 4 way junction. I can do all the cars at the whole junction using Thread Soth () but I do not need to crash cars in each other (see diagram)
What should I use?
-
Synchronization
-
Wait () / inform () / notify ALL ()
-
ThreadPanels
-
Canvas (BTW Canvas and what is its purpose? )
I have used the layerpane to insert images at the top of each other.
Here's 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 JLairPan LP = New Jellydner (); Private Jeppel red = new zeppel (); Private Jeppel Carbine = New Zeppel (); Private Emacs AnswerCard = New Imagine ("src / north.gif"); Private Image Icon usIcon = New Image Icon ("src / trafficLight.jpg"); Private JLab LB = new JLABEL (IICCON); Private JLabel lbcar_1 = new JLABEL (answercard); / * Private Image Icon South Factor = New Image ("src / trafficLight.jpg"); Private Imaging Westkare = New Imagen ("src / trafficLight.jpg"); Private Images Estkke = New Image Icon ("src / trafficLight.jpg"); * / Public Gui () {F. Setbound (0, 0, 655, 679); F.add (lp); Car_1.setOpaque (wrong); Car_1.setbounds (340, 120, 70, 105); //car_1.setBackground(Color.black); Car_1.add (lbcar_1); Red. Setbound (0, -5, 650, 650); Red.add (pound); 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 & lt; 540; i + = 1) {Thread.sleep (10); Car_1.setbounds (340, i, 70, 105); }} Hold (exception e) {}} public static zero main (string [] args) {Gui frame = new Gui (); }}
Any help is appreciated. Thanks for your time.
Many thanks to you
You can go there 4 shared resources Access needs to be synchronized: To cross the four corners of "junction", each car will first have to enter the first corner, then in the next corner.
However, if you lock each corner separately, you can reach the deadlock (due to the gridlock's compatibility example in you). This happens when all four cars ("lock") enter the closest class, and they all wait for the next watch car to clear the other attentiveness. See the link for the solution.
Considering the intersections, there is only one solution in the form of four resources. You can explain the whole intersection as a resource, as suggested ( In all respects, but not at all), although it does not mirror near the actual traffic flow. You can also establish synchronization rules for the four corners, which mirror the traffic lights.
Comments
Post a Comment