Threadlocal's Related introduction

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.