Threadlocal<t>
Brief introduction
Threadlocal mainly solves the problem that the data in multi-threading is inconsistent with concurrency. Threadlocal provides a copy of the data that is accessed concurrently in each thread, running the business through the access replica, which consumes memory, but greatly reduces the performance cost of thread synchronization and reduces the complexity of thread concurrency control. This is like in the Web project when a user queries the data, the incoming query conditions are overwritten by other users ' query criteria, so that the results of the query are not their own results.
Principle
There is a map in the Threadlocal class that stores a copy of the variable for each thread, the key of the element in the map is the thread object, and the value corresponds to the variable copy of the thread. Each thread can manipulate the replica variable that it creates, which is a local variable that is visible only to this thread. Each time a local variable in a thread can be obtained through the Get () function, the local variable in the thread is set with the set () function.
Introduction to the Connection database
This operation falls within the same thread when a business logic is to be processed by multiple DAO. So, we want to be able to put this connection in the thread or be associated with threads. You can use Threadlocal to save a copy of a variable within any thread, so that if the object exists, it can be used directly. If you place connection in threadlocal, ensure that the DAO class in the transaction gets the same connection, so that the transaction is guaranteed. Therefore, within the same thread, different DAO can be guaranteed to get the same connection.
Threadlocal's Related introduction