Sevlet thread-safety issues

Personally do not like to study when all things are unavoidably the record, I think the only important records on the line, the other by a short practice can quickly remember. Here the record about the thread-safety issues Sevlet;

First of all to understand, Sevlet engine for the first time only when the client sends a request to create Sevlet objects, which means that client requests are multiple instances of the same Sevlet by the process . So this time there are two cases:

1. If the request to access a local variable, then the local variable is part of the thread, there is no security problem, because every local variables are independent

2. If the request to access the global variables, that variable is part of this sevlet object, all threads can be changed, not independent, and this time there is thread-safety issues. When concurrent access, may A request to change the value of temp variable, then B request and change the value of temp variable, then A request to obtain the value of a variable, B request to obtain the value of a variable, it should not happen, because A, B request value acquired are changed twice. Solution:

(1): Use synchronized to change the code a variable with the lock, so that B will have to wait for a request to be processed after A request has been processed, so you can get to the correct value, but there is a problem quite like this All requests will then form a queue, the FIFO, the request must be processed one by one, this way time consuming too.

(2): Implement SingleThreadModel interface, it will be single-threaded mode processing, creates a Sevlet instance for each thread, but this feeling does not solve the essential problem, but creates an instance is also a maximum number of, at most, 20 more, more, then you suspend, the other said it, sevlet when it is created, will first determine sevlet have created is not a single-threaded (SignleThreadModel), is then on the newly created an instance, it is not, then loads the original examples, and this is the reason why the inherited SingleThreadModel interface will be able to create multiple instances.

But this does not really solve thread safety issues, and this method has also been abolished.

In summary, you can see sevlet not thread-safe, so try to avoid designing a global variable, is really so, use the above two methods, synchronous ,, do not understand.

 

Guess you like

Origin www.cnblogs.com/eenio/p/11231162.html