The LogicalCallContext of CallContext is a public variable in a multi-threaded environment, and a multi-threaded local variable

Source: Internet
Author: User

The LogicalCallContext of CallContext is a public variable in a multi-threaded environment, and a multi-threaded local variable

The root name has heard of this class. Here, we can see how to achieve unified management of Context data.

I thought that CallContext could be shared directly in a multi-threaded environment. Today I suddenly thought: Under the Asp. Net environment, setting is done. Will the object not be messed up by multiple clients?

With the following code testing:

static void Main(string[] args)        {            CallContext.LogicalSetData("key", "1");            Thread trd = new Thread(new ThreadStart(() =>            {                Console.WriteLine("first thread : " + CallContext.LogicalGetData("key"));                Thread.Sleep(1000);                Console.WriteLine("first thread : " + CallContext.LogicalGetData("key"));            }));            trd.Start();            Thread.Sleep(100);            CallContext.LogicalSetData("key", "2");            Thread trd1 = new Thread(new ThreadStart(() =>            {                Console.WriteLine("second thread : " + CallContext.LogicalGetData("key"));            }));            trd1.Start();            Console.Read();        }

The result is the bird's look:

 

It seems that I think too much, sweat...

Excerpt from an official note (from artech's blog ):

LogicalCallContext:The LogicalCallContext class is a version of the CallContext class used for method calling of Remote Application domains. CallContext is a dedicated collection object locally stored by a thread similar to a method call, and provides a data slot that is unique to each logic execution thread. The data slot is not shared between call contexts on other logic threads. When CallContext is propagated back and forth along the Execution Code path and checked by each object in the path, you can add the object to it. When you call a remote method for an object in another AppDomainThe CallContext class will generate a LogicalCallContext that is propagated together with the remote call.Only objects that expose the ILogicalThreadAffinative interface and stored in CallContext are propagated to the external AppDomain in LogicalCallContext. Objects of this interface cannot be transmitted together with remote method calls in the LogicalCallContext instance.

"The CallContext class will generate a LogicalCallContext that is propagated together with the remote call."

Here it should be said that only pass the past, regardless of the reference relationship, similar to "value transfer.

 

Thank you...


Is the getContext () method used to obtain the context variable? How to use it? Direct Context context = getContext ();? Me

Functions in java are actually a lot of encapsulated Code (the code is often used, so it is encapsulated into a simple function for you to call ), the process of calling a function is to execute the Code. In addition, it is built on a mobile phone and a java platform on a pda, generally, your java program is parsed through the platform into a program that can be run by the operating system. java programs are like gold, and it is impossible to directly buy things, the java parser is like a bank. You can use it if you change gold to a bank, and the operating system is like a country. You can change gold to USD or RMB, euro ....... the same is true for java. The parser compiles your java program into different programs in different environments to implement the cross-platform function, but xxMFC cannot be the same as java. It can only be used in windows, just as the same principle is that RMB cannot be directly purchased in the United States.

Several operations of ActionContextgetContext ()

ActionContext. getContext (). getSession () obtains the session, and then stores the corresponding value with put. This value remains available as long as the session is valid.

ActionContext. getContext (). put ("x", x) is a request by default, which is available before the request ends.

ActionContext. getContext (). getValueStack (). push (x). In Struts2, before a request reaches the Action method, the Action object itself will be pushed to ValueStack, and an object can be pushed directly to valuestack.

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.