We sometimes make multiple inquiries via token (pig: token is a key in Redis), such as:Once in the login interceptor, once in the controller's business query, so there is a waste of performance and resources problems!!!So how do you pass the data in the interceptor to the controller?There are two types of scenarios:1. Place the user object in the Request object2, implemented with threadlocal thread variables (objects are in the same thread before ente
Threadlocal can define thread-scoped variables, or thread-local variables. The difference from a general variable is that the life cycle is within the scope of the thread.That is, an object of a class (for a clear description, hereinafter referred to as a object) has a threadlocal variable,Then each thread is opened, and the first call to the A object will initialize the value of the variable and will not a
Threadlocal is used to implement data sharing within a thread, that is, for the same program code, multiple modules share one piece of data while running in the same thread, while another data is shared while running in another thread.Each thread invokes the set method of the global Threadlocal object, which is equivalent to adding a record to its internal map, where key is the respective thread, and value
In Dictdo_task_1 ()Do_task_2 ()Def do_task_1 (): std = global_dict[threading.current_thread ()] # does not pass in STD, but is found based on the current threaddef do_task_2 ()std = global_dict[threading.current_thread ()] # Any function can find out the STD variable of the current thread3) Way three:ThreadLocalImport threadingLocal_school = threading.local () # Create a global threadlocal objectDef process_student (): std = local_school.student
Use the Threadlocal class to implement thread-wide shared data:Make sure that each thread has its own private variable and takes its own variable. The following is implemented using the Threadlocal class, instead of the map usage in the previous lessonCode:Package org.yla.thread;import java.util.random;/** * Implement thread-wide private data using the Threadlocal
Werkzeug as a WSGI tool library, due to some considerations, it does not directly use the Python built-in threadlocal class, but instead implements a series of local classes. Includes simple local, and Localstack,localmanager and Localproxy implemented on this basis. Let's take a look at how these classes are used, the purpose of the design, and the specific implementation techniques.
Design of the Local class
Werkzeug's designers believe that Python
Familiar with the looper source of friends may know, looper and thread binding is not directly set in, but through threadlocal this thing indirectly to the looper of the reference to the current thread hold.Looper.javaprivate static void Prepare (Boolean quitallowed) {if (sthreadlocal.get () = null) {throw new RuntimeException ("Only one Looper could be created per thread");}Sthreadlocal.set (New Looper (quitallowed));}
As mentioned above, the variable security issue.
1. Try not to let the thread run on the same object instance. This is a relatively low-efficiency method. In some cases, sharing objects between threads is inevitable.
2. encapsulate variables that may be shared in a method to make it an internal variable of the method. In this way, every thread has its own private variable value even if multiple threads concurrently execute this method.
The second method is relatively recommended.
The
Place the connection in threadlocal to ensure that the DAO class in the transaction obtains the same connection, so as to ensure the transaction.
Below is a specific example of http://www.pin5i.com/showtopic-26854.html found online
Bytes -----------------------------------------------------------------------------------
Generally, we place the transaction processing on the business layer.
[1] Introduction
For example:Business logic method
Public A b
://os78f4hny.bkt.clouddn.com/20170710007.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>Operation Result:650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710008.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>Threadlocal class:Before you say Threadlocal class, look at a problem. If there are several methods of a, B, C,
ImportThreading#To create a global threadlocal object:Local_school =threading.local ()defprocess_student ():#gets the student associated with the current thread:std =local_school.studentPrint('Hello,%s (in%s)'%(Std, Threading.current_thread (). Name))defProcess_thread (name):#binding the threadlocal student:Local_school.student =name Process_student () T1= Threading. Thread (Target=process_thread, args= ('D
4.3 This section let's discuss some of the areas that need attention when it comes to the use of threadlocal.Threadlocal main use is GET, set, InitialValue These methods, specific use we do not introduce here, the following is just to give some of its use when the need to pay attention to the place.1. Null when the Get method is presentPackage com.ray.deepintothread.ch04.topic_3; Public classThreadlocalgetnull {Private intCount =0; PublicThreadlocalNewThreadlocal Public int GetCount() {returnInt
ThreadlocalBrief introductionThreadlocal 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
TLS (thread Local Storage) solves the mutex problem of multi-threaded access to critical resources by allocating more memory, that is, each thread has its own critical resource object, so there is no access violation, and no lock mechanism control is needed to compare the typical space-to-time strategy. The following is an example of Looper, which describes how TLS is implemented in Android. You can see several key points to the TLS technology: 1. A value (encapsulated hash table) is saved in ea
ThreadLocal: Binds only to the current threadInheritablethreadlocal: Passes the value to the child thread and shares the value with the child threadUse threadlocal, between parent and child threads, do not share valueFinal threadlocalTl.set ("Threadlocal-val");System.out.println ("Main-1:" + tl.get ());New Thread () {public void Run () {System.out.println ("Child
As can be seen from the name of Threadlocal, this is a thread-local variable, that is, only when the front-thread can access, since it is only when the front-thread can access the data, is naturally thread-safe. Public classThreadlocaldemo {Private StaticthreadlocalNewThreadlocal(); Public Static classParsedateImplementsRunnable {inti = 0; PublicParsedate (inti) { This. i =i; } /*** When an object implementing interface @seeThread#run ()*/@Ov
Threadlocal is not difficult to understand, I summarize the simplest understanding is:threadlocal Like other variables (local, global, Static) is also a variable type, but he is a thread variable, more bluntly he is a variable scope, that is, his scope is the current thread (such as a user's request to calculate a thread), Threadlocal is used to share between one thread. The
Load or delay load must be connected to the database, while in Java to connect to the database is dependent on Java.sql.Connection, in Hibernate session is the Connection layer of high-level package, A session corresponds to a connection, and it is necessary to have a session to implement lazy loading. And to delay loading, you have to be sure the same session. It is not possible to delay loading the proxy object of the previous session with another s
Oom?Java.lang.OutOfMemoryError:java head space heap memory overflowJava.lang.OutOfMemoryError:Permgen Space Java Eternal Overflow, that is, methods to overflow, generally because of the emergence of a large number of classes and JSPs, or the use of Cglib reflection mechanism,This can be resolved by changing the size of the method area and modifying it in the form of a similar-xx:permsize=64m-xx:maxpermsize=256m. In addition, too many constants, especi
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.