Servlet Tread tips

If your servlets only read from the request, write to the response, and save information in local variables, you needn't worry about the interaction among these threads. Once any information is saved in nonlocal variables,  however, you must be aware that each of these client threads has the ability to manipulate a servlet's nonlocal variables. (Java Servlet Programming, Jason Hunter)

we can deal with this problem in 4 methods:

1. in the declare, use " public synchronized void doGet(...,...)"

2.in the function, use " synchronized(this){ //do things about nonlocal variables} "

3.in the function, assign the value of nonlocal variable to a (temp) local variable, then after " synchronized(this){ //do things about nonlocal variables, and assign the value of nonlocal variable to a (temp) local variable} "

4.  decide that we are willing to suffer the consequences of ignoring synchronization issues.

你可能感兴趣的:(servlet)