-------------- Java training for Android Training
We look forward to communicating with you! --------------
After reading the program compiled by Mr. Zhang, it was really difficult to find out, but I just couldn't think of it,
Only to discover the programming idea is the most important
Through the object-oriented example given by the instructor, I have benefited a lot. It seems that I have a little understanding. It reminds me of the validate method in formbean of struts1. Why is the validate method defined in formbean, only now can we find out that this is the case, because it is just like what instructor Zhang says who owns the data that provides external operations on the data. take the struts verification method. Because formbean has fields to be verified, it is not suitable to define the validate method here. If it is defined in another class, it is a little troublesome, because we need to obtain the fields to be verified from formbean.
Teacher Zhang's analysis shows that thinking is the most important thing. At the same time, he must observe his life. This is a great benefit to programming, because the purpose of programming is to serve life, if a programmer is not familiar with life, how can he write a program? So from now on, let's take a look at our daily lives.
It was a bit difficult to understand at first, and then I thought it was really the case.
There are three main classes from this system:
Road Lamp lampcontroller
Road class:
There are two thread pools in the road class. The first thread pool adds 999 vehicles to the road.
The second thread pool is a periodic execution task, that is, a task is executed at intervals.
Executorservice is equivalent to a thread pool. Tasks in the run method are handed over to the entire thread pool, not just a thread.
If an exception occurs during any execution of a task, the subsequent execution will be canceled. Otherwise, the task can only be canceled or terminated through a program.
After a thread pool is used up, call the shutdown method of the thread pool to start the shutdown sequence of the thread pool. After the shutdown method is called, the thread pool will no longer accept new tasks, however, after all previously submitted tasks are completed, all the threads in the pool will die. In addition, the thread pool will be closed by calling the shutdownnow method, this method is used to stop all active tasks that are being executed, pause tasks that are waiting, and return to the list of tasks that are waiting for execution.
For example:
// Implement the runnable interface to define a simple Thread class
Class testthread implements runnable
{
Public void run ()
{
For (INT I = 0; I <100; I ++)
{
System. Out. println (thread. currentthread (). getname ()
+ "The I value is:" + I );
}
}
}
Public class threadpooltest
{
Public static void main (string [] ARGs)
{
// Create a thread pool with a fixed number of threads (6)
Executorservice pool = executors. newfixedthreadpool (6 );
// Submit two threads to the thread pool
Pool. Submit (New testthread ());
Pool. Submit (New testthread ());
// Close the thread pool
Pool. Shutdown ();
}
}
The runnable implementation class created in the above program is not much different from the maximum thread pool created at the beginning. After the runnable implementation class is created, no threads are directly created and the thread is started to execute the runnable task, instead, the thread pool is used to execute the task and the thread pool is used to execute the task. The running result is as follows:
The following is the lamp class.
In lamp enumeration, why is the lamp in the constructor a string? Because the lamp in the constructor is defined below, it cannot be used before. Therefore, parameters are defined by the Instance name,
For the meanings of enumeration constructor parameters, only one example is provided:
S2n ("N2s", "s2w", false) N2s indicates the lamp corresponding to s2n. s2w indicates the next lamp of s2n. If s2n is green, the lamp corresponding to s2n is green.
In addition, the enumerated valueof method is used to convert a string into an enumerated object.
System. Out. println (name () + "lamp is green, there should be a total of six directions to see the car pass"); why are there six directions to see the car pass?
It was strange at first that four directions on the road had been green, that is, the turns to the right in each direction were green, the lights corresponding to this direction have a total of 6 directions with cars passing through.
This code snippet was written in this way,
Lamp nextlamp = lamp. valueof (next );
If (next! = NULL ){
System. Out. println ("green light from" + name () + "------> switched to" + next );
Nextlamp. Light ();
}
Return nextlamp;
In this way, it is possible that next is null, so writing according to the above code snippets is safe. If we can find it at the beginning, the success of our program will be greatly increased, we may not want to be as competent as Teacher Zhang,
Therefore, we can summarize a rule. If a method requires a parameter, this parameter becomes the first condition for calling the method. First, we must consider whether the parameter exists or is null, it is best to be able to judge. if we can prevent problems before they happen, we can avoid a lot of trouble and enhance the robustness of the program.
Lamp controller:
First, we need to define a member variable such as the current green light. during initialization, the green light is s2n.
Then, define a thread pool and execute tasks cyclically every 10 seconds. Use the lamp blackout method to hold the program red light. His return value is the next green light, use the current green light to record the next green light.
We should also pay attention to why the next green light should be returned for blackout? Just like the Remove Method of the list set, it returns the elements of the deleted set. Because you have deleted it, you must use it.
The last is a test class:
First, initialize the name of the path in 12 directions, and then start the controller of the lamp. The whole system becomes a whole.
-------------- Java training for Android Training
We look forward to communicating with you! --------------