In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to treat the java multithreaded traffic light simulation process, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Thread Design of Traffic Lights
Objective: multi-thread design, synchronization mechanism to design a traffic signal class: variables: location, color (red, yellow, green), display time (seconds). Method: switch signal lights. Create and start two threads (east-west, north-south) running at the same time. The experiment requires the design of threads. Design the schematic interface of traffic lights at the intersection. The traffic lights in each direction are further divided into three kinds of lane lights: left turn, straight ahead and right turn. The fuzzy control of time is carried out according to the traffic flow.
At the beginning of the course design, I didn't look at the teacher's request carefully, only knew that it was the traffic light. Then we began to look for materials. Baidu and a large number of traffic light designs on the Internet refer to teacher Zhang Xiaoxiang's tutorials. The general design method is to create three classes: lamp, road, and lampcontrol.
But the goose. That's right. But the goose came again, and after looking up the information for about two days, I looked at the design requirements issued by the teacher again. This, just control the lights, not the car, cover your face.
So the design idea immediately became clear. According to Mr. Zhang's practice, there are three lights at each intersection, namely, left, straight and right, so there are a total of 12 lights. According to the requirement, the right turn light is always bright, and the left turn and straight light have alternation of red and green. Among the eight lights excluding the right turn lights, they can be divided into four groups of lights that can be matched in pairs. they are east-west left turn north-south (parallel turn vertical), north-south left turn north-south (vertical traffic), north-south left turn east-west parallel (parallel traffic).
So I numbered the 12 lights:
Emmm is probably like this, but unlike the traffic lights in life, life is always looking at the lights at the opposite intersection. We are here to simulate. Just use the lights at each intersection.
Give me a general explanation
Light 1 is a left light from south to west, light 2 is a straight light from south to north, and light 3 is a right light from south to east.
No. 4 light is a right turn light from east to north, No. 5 light is a straight light from east to west, and No. 6 light is a left light from east to south.
No. 7 is a right turn light from north to west, No. 8 is a straight light from north to south, and No. 9 is a left turn light from north to east.
The No. 10 light is a left turn light from west to north, No. 11 is a straight light from west to east, and No. 12 is a right turn light from west to south.
Then, according to the principle of matching, this is probably the picture:
We can get:
These four sets of lights.
At this point, our thinking is very clear, we can open a process for each set of semaphores, and then make the four processes cycle alternately to achieve the green light conversion. The synchronization technology of the program is used.
Of course, we have already driven four processes, for the sake of icing on the cake, I certainly don't mind adding a car, hhhhhh.
The picture of the experimental results:
Source code:
Light class:
Package traffic; public class light {int xdyy; / / the position of the lamp on the canvas boolean status; / / the state of the lamp public light (int xreint yno Boolean islight) {this.x=x; this.y=y;this.status=islight;} public void setlight (boolean sta) {/ / external interface to change the status of the lamp this.status=sta;}}
Lamp class:
Package traffic; public class lamp implements Runnable {light opposite, now; / / matching two lights int greentime; / / Green time int name; / / grouping boolean status; / / status static Object lock = new Object (); public lamp (light 10, light 12 drawing int gt, boolean st, int name) {now = 10 / 10 leading status = st;greentime = gt;this.name = name;lightstatues ();} public void change () {this.status =! (this.status); lightstatues () } public void setgreentime (int time) {this.greentime=time;} public int getgreentime () {return this.greentime;} public void lightstatues () {opposite.setlight (status); now.setlight (status);} public void run () {while (true) {synchronized (lock) {/ / use synchronized to implement mutually exclusive if (name = = mainclass.panel.islight) {/ / use auxiliary variables to achieve sequential cycle / / System.out.println ("now is:" + name) Change (); mainclass.panel.repaint (); try {Thread.sleep (greentime);} catch (InterruptedException e) {e.printStackTrace ();} lock.notifyAll (); / / Wake up other processes change (); mainclass.panel.LampChange (); mainclass.panel.repaint (); try {lock.wait ();} catch (InterruptedException e) {e.printStackTrace ();}} else {try {lock.wait (); / / suspend process} catch (InterruptedException) {e.printStackTrace ();}
Mypanel class:
Package traffic; import java.awt.Color;import java.awt.Graphics;import java.util.ArrayList;import java.util.Random;import javax.swing.JPanel;public class Mypanel extends JPanel {light 10 lamp lamp1,lamp2,lamp3,lamp4; / / four sets of semaphores static int islight; / / implement the auxiliary variable Random r=new Random () of the process cycle; static public ArrayList lightlist= new ArrayList () / / use list to easily traverse each light static public ArrayList carlist= new ArrayList (); / / facilitate traversing each car public Mypanel () {/ / TODO Auto-generated constructor stubl0=addlist (l0,212,316, false); l2=addlist (12242,316, false); l3=addlist (l3,272,316, true); l4=addlist (l4,316,116, true); l5=addlist (l5,316,146, false); l6=addlist (16,316,176, false); l7=addlist (l7116,70, true); l8=addlist (l8,146,70, false) L9=addlist (l9,176,70, false); l10=addlist (L10,70,2122false); l11=addlist (11,70,242, false); l12=addlist (L12,70,272, truelamp1=new lamp); lamp2=new lamp (L11 lamp3); lamp3=new lamp (L10Power2000); lamp4=new lamp (L8 Thread t1=new Thread (lamp1); / create and start threads Thread t2=new Thread (lamp2); Thread t3=new Thread (lamp3); Thread t4=new Thread (lamp4) T1.start (); t2.start (); t3.start (); t4.start ();} light addlist (light aline int xreint yrep Boolean sta) {a=new light (xmemyresta); lightlist.add (a); return a;} public void addcar () {/ / generate car int now,next;now=r.nextInt (4); next=r.nextInt (4); car testcar=null;while (now==next) next=r.nextInt (4) Switch (now) {case 0:testcar=new car (now,next,l0,l2,l3); break;case 1:testcar=new car (now,next,l6,l5,l4); break;case 2:testcar=new car (now,next,l9,l8,l7); break;case 3:testcar=new car (now,next,l10,l11,l12); break;} carlist.add (testcar); Thread catt=new Thread (testcar); catt.start ();} public void LampChange () {islight= (islight+1) 4 } public void paint (Graphics g) {super.paint (g); g.setColor (Color.darkGray); / / g.fillRect (0100,400,6); g.fillRect (0300,400,6); g.fillRect (100,0,6,400); g.fillRect (300,0,6,400); g.setColor (Color.gray); g.fillRect (0200,400,2); g.fillRect (200,0,2400); g.setColor (Color.blue) G.setColor (Color.black); / / draw signal lamp board g.fillRect (202jue 306,100,40); g.fillRect (306dint 106,40,100); g.fillRect (106dint 60,100,40); g.fillRect (60minus 202jin40,100); light temp;car buf;for (int itemo Tinci)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.