Analysis and implementation of traffic light management system of Dark Horse programmer-java Learning

Source: Internet
Author: User
Tags semaphore

--------Android Training, Java training, look forward to communicating with you! --------

Traffic Light Management system:

Demand:
Simulate the intersection of traffic lights management system logic, the specific requirements are as follows:
1. Asynchronously randomly generates vehicles that follow each route.
For example:
Vehicles from south to north turn right.
From the east to the south of the vehicle enumeration to achieve. Enumeration is a special (class). Because there are methods, fields, and construction methods
The code is implemented as follows:

//Package com.fylx.test; Public enumlamp{//the light of the straight line, after the straight line lights up, left turn lights again light, left turn the light turns red, another straight line of lights to brightenS2N ("n2s","s2w",false), s2w ("n2e","e2w",false), e2w ("w2e","E2s",false), E2s ("w2n","s2n",false),    //Turn off the lights on the left-hand side of the line and switch on the light from the straight lineN2s (NULL,NULL,false), N2E (NULL,NULL,false), W2E (NULL,NULL,false), w2n (NULL,NULL,false),    //Turn the light on the right line, assuming that all the lights on the right-turn route are long-litS2E (NULL,NULL,true), E2N (NULL,NULL,true), n2w (NULL,NULL,true), W2s (NULL,NULL,true); PrivateString opposite; PrivateString Nextlamp; PrivateBoolean isgreen; //returns whether a light is a green attribute     PublicBoolean Isgrean () {return  This. Isgreen; }    //The head lamp of each beacon, the next light to turn green, whether the lamp is greenLamp (String opposite, String Nextlamp, Boolean isgreen) { This. opposite =opposite;  This. Nextlamp =Nextlamp;  This. Isgreen =Isgreen; }    //The way to green, if you have the head lamp, the state of the head lamp to turn green     Public voidTurngrean () { This. Isgreen =true; System. out. println ( This. Name () +"the light in the direction turns green!"); if( This. opposite! =NULL) {lamp.valueof ( This. Opposite). Turngrean (); }    }    //Red method, if there is the next corresponding lamp, then the next light will be green, and return to the next light     PublicLamp turnred () { This. Isgreen =false; System. out. println ( This. Name () +"the light in the direction turns red!"); if( This. opposite! =NULL) {lamp.valueof ( This. Opposite). turnred (); } Lamp Next=NULL; if( This. nextlamp! =NULL) {Next= Lamp.valueof ( This. Nextlamp); Lamp.valueof ( This. Nextlamp). Turngrean (); }        returnNext; }}//

Below we analyze the signal controller, from the demand can be seen, the controller is to control the signal exchange time, as long as a timer, in the specified time and time interval constantly let the lights in a coherent light up the transformation can be
And from the above signal, we can see that the signal at the time of the change, will automatically trigger its corresponding opposite the signal and the change of the left turn, so, in the controller, we just control a lamp to turn red to green on it,
The code is implemented as follows:

//Package Com.fylx.test;import java.util.concurrent.*; Public classcontroller{//the current signal to be controlled by the controller    PrivateLamp Currentlamp;  PublicController () { This. Currentlamp =lamp.s2n; }    //enable the semaphore controller, execute after 5 seconds, and change the color of the beacon every 10 seconds     Public voidOn () { This. Currentlamp.turngrean (); Scheduledexecutorservice Timer= Executors.newscheduledthreadpool (1); Timer.scheduleatfixedrate (NewRunnable () { Public voidrun () {Currentlamp= Controller. This. currentlamp.turnred (); }        }, 5,Ten, Timeunit.seconds); }}//

Route class analysis, from the demand, the route class just follow the change of traffic lights, the increase and appearance of the car is asynchronous random generation. And every car through the intersection of time is 1 seconds, can walk the car to see the corresponding signal is not green, and the function of the route
is not associated with the signal controller, and it is associated with a semaphore, because our lights are implemented with enumerations, so the route class directly save the name of the signal, because there is a car on the route, so the route of the class also add the relevant car code
The code is implemented as follows:

//Package Com.fylx.test;import java.util.*; import java.util.concurrent.*; Public classroute{//Define a vehicle    PrivateList<string> vechicles =NewArraylist<string>(); //The name of the line, the request and the name of the semaphore correspond    PrivateString name; //Random-count operatorsRandom random =NewRandom ();  PublicRoute (String name) { This. Name =name; //start a thread pool, asynchronous random production vehiclesExecutorservice Service =Executors.newsinglethreadexecutor (); Service.execute (NewRunnable () { Public voidrun () { for(inti =1; I < +; i++)                {                    Try{thread.sleep (Random.nextint (Ten) +1) * +); } Catch(interruptedexception e) {e.printstacktrace (); } Route. This. Vechicles.add (Route. This. Name +"_"+i);        }            }        }); //define timers, execute in 1 seconds, and cross a car every 1 secondsScheduledexecutorservice timer = Executors.newscheduledthreadpool (1); Timer.scheduleatfixedrate (NewRunnable () { Public voidrun () {if(Vechicles.size () >0) {lamp lamp= Lamp.valueof (Route. This. Name); if(Lamp.isgrean ()) {System. out. println (Vechicles.remove (0) +"Security's through!"); }                }            }        }, 1,1, Timeunit.seconds); }}//

Now let's simulate a test environment in which there is a route, there is a signal controller, because the semaphore is an enumeration, do not instantiate here, directly in the controller associated with a semaphore can be.
The test code is as follows:

//Package com.fylx.test; Public classtest{ Public Static voidMain (string[] args) {//TODO auto-generated Method StubString[] Lamps =NewString[] {"s2n","s2w","e2w","E2s","n2s","n2e","w2e","w2n","s2e","e2n","n2w","W2s" };  for(String lamp:lamps) {//according to the signal, instantiate the corresponding line            NewRoute (lamp); }        //production of a signal controller, and simultaneously enable        NewController (). on (); }}//

In this system design process, the difficult is not the code implementation, but the business analysis, if the business logic analysis is clear, the code realization also is good to write.

Analysis and implementation of traffic light management system of Dark Horse programmer-java Learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.