Java Multithreading-thread closure

Source: Internet
Author: User

  thread closure: When accessing shared mutable data, synchronization is usually required. One way to avoid synchronization is to not share data. If data is accessed only on a single-wire range, synchronization is not required, a technique known as thread closure (threads confinement)

Thread closure Technology A common application is the JDBC Connection object, and the JDBC specification does not require that the connection object be thread-safe, and in a server application, the thread obtains a connection object from the connection pool. Returns the object to the connection pool when it is finished. Here are a few thread-blocking techniques:

  1. Ad-hoc Thread closure

Ad-hoc thread closure means that the responsibility for maintaining a thread's closeness is fully assumed by the program implementation, is very fragile, so use less in the program, generally use stronger thread blocking technology, such as the stack closed or threadlocal class.

  2, Stack closed  

A stack closure is a type of thread closure that can only be accessed by local variables in a stack closure. One of the intrinsic properties of a local variable is that it is enclosed in the execution stack, and other threads cannot access the stack, and the stack closure is also known as thread internal use or thread-local use. In short, it is a local variable. Multiple threads access a method, and the local variables in this method are copied one minute to the line stacks. So local variables are not shared by multiple threads, and there is no concurrency problem. So we can use local variables to not use global variables, global variables easily cause concurrency problems.

For example, the following:

1  Public intLoadtheark (collection<animal>candidates) {  2Sortedset<animal>animals; 3         intNumpairs = 0; 4Animal candidate =NULL; 5           6         //animals are encapsulated in methods, do not overflow them7Animals =NewTreeset<animal> (Newspeciesgendercomparator ()); 8 Animals.addall (candidates); 9          for(Animal a:animals) {Ten             if(candidate==NULL|| !Candidate.ispotentialmate (a)) {   Onecandidate =A;  A}Else{   -Ark.load (NewAnimalpair (candidate,a));  -++Numpairs;  thecandidate =NULL;  -             }   -         }   -         returnNumpairs;  +}
View Code

Instantiates a TreeSet object in Loadtheark and saves a reference to the object in animals. At this point, there is only one reference to the collection animals, which is enclosed in a local variable and is therefore enclosed in a local variable. However, if a reference to the collection animals (or any internal data within that object) is published, the closure is destroyed and the object animals escaped.

  3, Threadlocal class

  

Java Multithreading-thread closure

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.