Thread security and security issues
1. What is thread security?
When it comes to thread security, the program must be running in a multi-threaded environment. Is there a thread security issue in the code of a multi-threaded application? The answer is obviously no. For example, the Action we write is a code running in a multi-threaded environment. When the web server receives an http request, it creates a Thread to process the request, but why is there no way to add synchronization to our actions? The reason is that our action object, every time we receive an http request (a Thread), will re-create (new) a new action object, the action objects used by different threads are different. After action, let's talk about service. If spring is used in the project, we all know that the object in the spring container is single by default, that is, there is only one service object in the program, when a service object is called by different threads, a service object is used. Therefore, in general, a service object should be a stateless object, that is, it does not contain attributes. If in some cases, you define an attribute for a service, the service method reads and writes this attribute. Do you need to consider thread security issues? The answer is very necessary!
This attribute you define is a resource shared by multiple threads. When multiple threads have read/write operations, it is possible (note that it is possible) to have thread security problems.
To sum up the methods for judging whether a program has thread security problems:
1. Is the program running in a multi-threaded environment?
2. Does multithreading share a resource and perform read and write operations on the shared resource?
If the preceding two points are met, it is necessary to consider thread security issues.
So the above Code is to consider thread security issues. But you may say why I know there may be thread security issues, but I have not done any synchronization processing? The reason is that thread security does not necessarily cause thread security issues. It seems a little contradictory. Because my business determines that there is no thread security problem here, or even if there is a thread security problem, the result is predictable and will not affect the system. So I didn't perform synchronization control here. Therefore, it is necessary to judge whether there is a thread security problem in the memory, and determine the cause based on the business characteristics and the results caused by the problem.
Ii. How to Solve thread security problems
1. Set the object to stateless
For example, if you try not to set properties for the above service object, the service object is stateless and there is no shared resource problem when multithreading uses this service object.
2. Use a local object
If activeTimer is an object in a method, there is no thread security problem, because when multiple threads call the same object method, the objects in the method are private.
3. If attributes have to be used in an object, for example, the above activeTimer must be set to the service object attribute, you can consider using the ThreadLocal packaging attribute. The encapsulated activeTimer is thread-safe, threadLocal actually creates an activeTimer object for each thread to reproduce. Each thread uses its own activeTimer to avoid thread security issues. Of course, the activeTimer modified by each thread is not shared, use it according to your business characteristics.
4. When the above three methods are not suitable, the ultimate trick is to use the thread synchronization technology. Lock the code block for reading and writing shared resources, so that multiple threads can access the Code in order, so that there is no thread security. There are two main methods for implementing thread synchronization (mutex) in Java: synchronized and lock.