Easily use thread 1 zt - today find this to see it.

zhaozj2021-02-16  79

Threading lightly: do not share sometimes the best Date: 2004-02-19 Author: Brian Goetz Read Count: 1290 use ThreadLocal improve scalability

The Threadlocal class is quietly appearing in the Java platform version 1.2. While supporting thread part variables is part of many thread tools (such as POSIX PTHREADS tools, the initial design of the Java Threads API does not have this useful feature. Moreover, the initial implementation is also quite inefficient. For these reasons, Threadlocal is very concerned, but it is very convenient to simplify thread security concurrent procedures. In Part 3 of the thread, the Java Software Consultant Brian Goetz studied Threadlocal and provided some skills. Participate in the Brian multi-thread Java programming discussion forum to get help from threads and concurrency issues in your project.

Write a thread security class is difficult. It not only requires careful analysis of what conditions can read and write variables, but also require careful analysis of how other classes can use a certain class. Sometimes, it is difficult for classes to become thread security without affecting the functions, ease of use, or performance. Some classes reserve status information called from a method to the next method, which is difficult to make such a class a thread security in practice.

Managing Non-threaded security classes is more easier than trying to make classes to become thread security. Non-thread security classes can usually be used safely in multi-threaded programs, as long as you can ensure that the instances of a class used by a thread are not used by other threads. For example, the JDBC Connection class is non-thread security - two threads cannot be safely shared on the small size level. However, if each thread has its own Connection, then multiple threads can safely perform database operations.

It is of course possible to maintain a separate JDBC connection for each thread (or any other object) for each thread; Thread API gives us all the tools you need to connect objects and threads. Threadlocal allows us to easily link threads and its per-thread data.

What is thread local variable (Thread-Local Variable)? Thread local variables efficiently provide a copy of the single thread partial variable value for each thread using it. Each thread can only see the value associated with you, instead of other threads may be in use or modify their own copy. Some compilers (such as Microsoft Visual C Compiler, or IBM XL Fortran Compiler) use storage class modifiers (like static or volatile) to integrate support for thread local variables into their language. The Java Compiler does not provide special language support for thread local variables; vice versa, it implements these support with Threadlocal classes, and there is special support in the core THREAD class.

Because the thread part variable is implemented by a class, not as part of the Java language itself, the use syntax of the Java language thread local variable is awkward than the use syntax of the inner construction line local variable language. To create a thread local variable, instantiate an object of THREADLOCAL. The behavior of Threadlocal classes is similar to the behavior of various Reference classes in java.lang.ref; the ThreadLocal class acts as a indirect handle that stores or retrieves a value. Listing 1 shows the Threadlocal interface.

Listing 1. Threadlocal interface public class threadlocal {public object get (); public void set (Object newValue); public object initialaging ();} GET () Accessor retrieves the value of the current thread of the variable; set () Accessor Modify the current The value of the thread. The initialvalue () method is optional. If the thread is not used a variable, you can use this method to set the initial value of this variable; it allows delay initialization. Implementing a sample implementation to explain that Threadlocal's work is the best way. Listing 2 shows an implementation of Threadlocal. It is not a particularly good implementation (although it is very similar to the initial implementation), it is very likely that it is poor, but it clearly illustrates Threadlocal's working mode.

转载请注明原文地址:https://www.9cbs.com/read-16414.html

New Post(0)