Thread-Local provides get and set access or methods that maintain a separate copy of the value for each thread that uses it, so a get returns the most recent value passed to set from the currently executing thread.
比如:
private static ThreadLocal<Connection> connectionHolder
= new ThreadLocal<Connection>() {
public Connection initialValue() {
return DriverManager.getConnection(DB_URL);
} };
public static Connection getConnection() {
return connectionHolder.get();
}
本文介绍了ThreadLocal的工作原理,展示了如何使用ThreadLocal为每个线程提供独立的数据副本,并通过一个数据库连接池的例子来说明其实际用途。
1738

被折叠的 条评论
为什么被折叠?



