Servlet thread safety issues

What is the thread-safety issues

1. Access presence multithreading
2. Modify the presence of shared database
user when a plurality of threads simultaneously modify the same data, the data will be modified to modify the coverage data, the data can not be read to modify own data modifications.

Data analyzed for the presence of the JVM thread-safety issues

1. Stack memory
stack memory is a multi-column, that will create a JVM stack for each thread, in which the data will not be shared. Additional methods of local variables in a stack frame Stack, the method is finished, the stack frame pop the stack, local variables disappear. Thread safety does not exist.
2. heap memory
a JVM exists only a heap memory, and is shared objects created in the heap memory, in which the member variables may be accessed by multiple threads. Modify these values, thread-safety problems.
3. The method area
a JVM there is only one method area, static variables and constants in the method area. Constants can not be modified, does not exist, and static variables are shared by multiple threads, there is a security thread static variable.

Solve the servlet thread safety issues

1. The definition of member variables are not accessible, use local variables.
2. Use thread synchronization mechanism synchronized.
3. Create a counter to record the number of threads access to control the thread-safety issues.

Published 114 original articles · won praise 8 · views 5486

Guess you like

Origin blog.csdn.net/OVO_LQ_Start/article/details/104734069