Thread-safe definitions are often confusing, and search engines will find countless definitions, such as:
- Thread-Safe code for multiple threads to execute simultaneously and to work correctly
- It is thread-safe code for multiple threads to execute concurrently to manipulate shared data in the right way.
And there are a lot of similar definitions.
Do you think that definition actually doesn't make any sense and makes people more confused? While these definitions are true, the fact is that they do not provide any practical help or opinion. How do we differentiate between thread-safe classes and unsafe classes? What do we mean by "security"?
what is the correctness of thread safety?
The core of any reasonable definition of thread safety is the concept of correctness. Therefore, before we understand thread safety, we should first understand this "correct".
Correctness means that a class conforms to its specifications.
A good class specification will have all the information about the state of a class at any given time, performing certain operations on it, and its post conditions. But we often do not write enough specifications for our class, how can we know that they can be used correctly? We can't, but that doesn't stop us from using them once we convince ourselves that "code works". This "code confidence" comes from our proximity to the right.
Optimistically defines "correctness" as something that can be identified, and now we can define thread safety in a less-than-a-round way: It is thread-safe when a class continues to function when it is accessed from multiple threads .
This class is thread-safe, regardless of how the run-time environment thread is interleaved, only if it behaves correctly from multiple threads, and the part of the calling code does not have additional synchronization or other coordination.
If this broad "correctness" makes you feel bored, you might think that the thread-safe class is a worse class in a concurrent environment than a single-threaded environment. The thread-safe class encapsulates the required synchronization so that the client does not have to provide its own synchronization.
Example: no statusservlet
A good example of thread-safe classes is that Java Servlets have no fields and references (references), and there are no fields from other classes, and they are stateless.
Public class Implements Servlet { publicvoid Service (servletrequest req, Servletresponse resp) { = extractfromrequest (req); = factor (i); Encodeintoresponse (resp, factors);} }
The data is stored only on the stack that is executing the thread, that is, the local variable, in a particular calculation. One thread accessing a statelessfactorizer does not affect the result of another thread accessing the same statelessfactorizer, because two threads do not share state as if they were accessing different instances. Stateless objects are thread-safe because the access operations of one thread to stateless objects do not affect the correctness of operations in other threads.
This is a small and important concept around thread safety.
Happy Learning!!
Java Concurrency – Thread safety?