Thread Continue - 2

zhaozj2021-02-16  62

Listing 2. Threadlocal's bad implementation

Public class threadlocal {private map value = collections.synchronizedmap (new hashmap ());

public Object get () {Thread curThread = Thread.currentThread (); Object o = values.get (curThread); if (o == null && values.containsKey (curThread)!) {o = initialValue (); values.put Curthread, O);} Return O;

Public void set (Object newValue) {VALUES.PUT (thread.currentthread (), newvalue);}

Public Object InitialValue () {return null;}}

This implementation will not be very good because each get () and set () operations require synchronization on the Values ​​mapping table, and if multiple threads accesses the same Threadlocal, it will occur. In addition, this implementation is not practical because the keywords in the VALUES map table with Thread objects will result in garbage recycling for Thread after threads, and cannot be made to thread-specific values ​​for the thread of Threadlocal of the dead thread. Garbage recycling.

Using ThreadLocal Each thread Singleton thread local variable is often used to depict a shared object with state "single sub) or thread secure, or by encapsulating unsafe variables into threadlocal, or by putting objects The state of the thread is encapsulated into Threadlocal. For example, in an application that is closely linked to the database, many methods of programs may need access to the database. In each method of the system, it contains a connection as a parameter. It is inconvenient - using "list" to access the connection may be one although more rough, but it is more convenient. However, multiple threads cannot securely share a JDBC Connection. As shown in Listing 3, by using Threadlocal in "单", we can make any classes in our programs to get a reference for each thread Connection. This way, we can think that Threadlocal allows us to create each thread list.

Listing 3. JDBC connection to a per-thread Singleton stored in a public class ConnectionDispenser {private static class ThreadLocalConnection extends ThreadLocal {public Object initialValue () {return DriverManager.getConnection (ConfigurationSingleton.getDbUrl ());}}

Private threadlocalconnection conn = new threadlocalConnection ();

Public static connection getConnection () {return (connection) conn.get ();}}

Any cost-to-expensive amount of cost is relatively expensive or non-threaded security, such as JDBC Connection or Regular expression matching, is a good place for each thread list. Of course, in this place, you can use other technologies, such as with a pool to securely manage sharing access. However, from the perspective of scalability, there are some potential defects even with pools. Because the pool implementation must use synchronization to maintain the integrity of the pool data structure, if all threads use the same pool, then in systems that are frequently accessed frequently, the program performance will decrease due to dispute. Simplifying the debug log Record for Threadlocal Other applications that are suitable for use Threadlocal but use pools that cannot be a good alternative technology, including storage or accumulating each thread context information for later retrieval. For example, suppose you want to create a tool for managing multithreaded application debugging information. You can accumulate debugging information as a thread partial container as a thread partial container as a thread. At the beginning of a work unit, you empty the container, and when an error occurs, you query the container to retrieve all the debug information generated by this work unit so far.

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

New Post(0)