Association of ThreadLocal with Request and Session

Source: Internet
Author: User

ThreadLocal <T> the class row is. NET Framework 4.0 is supported. MSDN only provides a simple explanation: it provides local thread storage for data, and the instance Code provided by it is not very good, for demonstration purposes. So we don't know what this type is.

 

I. Application of ThreadLocal in JAVA

In fact, this type already exists in JAVA and has been widely used in JAVA systems. Now we describe these concepts with. NET.

Let's take a look at the JAVA code. This code exists in Hibernate:

Private static final ThreadLocal threadSession = new ThreadLocal ();
Public static Session getSession () throws InfrastructureException {
Session s = (Session) threadSession. get ();
Try {
If (s = null ){
S = getSessionFactory (). openSession ();
ThreadSession. set (s );
}
} Catch (HibernateException ex ){
Throw new InfrastructureException (ex );
}
Return s;
}

To understand or understand the meaning of this code, we need to understand how the General WEB Application Server Processes requests and threads:

Will a Thread be generated for one request? No, WEB engines (such as ASP. NET engine) will maintain a managed thread pool, multiple requests may use a thread (PS: strictly speaking, it is asynchronous, not a thread, but it is packaged as a hosting thread ).

Continue with the code: First, check whether the current thread has been put into the session. If not, use sessionFactory (). openSession () creates a session and then sets the session to the thread. It is actually placed on the ThreadLocal object of the current thread. Note that,This session cannot be obtained in other threads..

 

Ii. ThreadLocal performance in. NET

First, the two types are inconsistent in the API declaration, but the purpose is the same:

Let each thread maintain its own variable.

Now, write a piece of code to test:

Public partial class Handler: System. Web. UI. Page
{
Private static ThreadLocal <Sample> sampLocal = new ThreadLocal <Sample> ();
Protected void Page_Load (object sender, EventArgs e)
{
If (! SampLocal. IsValueCreated)
{
SampLocal. Value = new Sample ();
}
Response. Write ("Thread. CurrentContext. ContextID =" + Thread. CurrentContext. ContextID + "<br/> ");
Response. Write ("Thread. CurrentThread. ManagedThreadId =" + Thread. CurrentThread. ManagedThreadId + "<br/> ");
Response. Write ("sampLocal =" + sampLocal. Value. GetHashCode () + "<br/> ");
}
}

Public class Sample
{
}

The most likely output is:

Thread. CurrentContext. ContextID = 0
Thread. CurrentThread. ManagedThreadId = 9
Samplocal= 57902434

Keep refreshing. If threadid is 9, The sampLocal is the same object.

3. ThreadLocal and static

It can be understood

1: ThreadLocal is the static variable in the thread. Maybe it is better to name it ThreadStatic?

2: static is a variable that can be shared by all threads.

Iv. Use of ThreadLocal

So why is this code designed based on one point:

Avoid the access method passed by parameters, but note that get () is the same shared object, and the concurrent access problem must be solved by other means;

 

Reference: ThreadLocal

Related Article

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.