ThreadLocal 用途与使用场景

ThreadLocal,即线程变量,是一个以 ThreadLocal 对象为键、任意对象为值的存储结构。

概括起来说,对于多线程资源共享的问题,同步机制采用了“以时间换空间”的方式,而 ThreadLocal 采用了“以空间换时间”的方式。前者仅提供一份变量,让不同的线程排队访问;后者为每一个线程都提供了一份变量,因此可以同时访问而互不影响。

ThreadLocal 类主要有如下方法:

  • protected T initialValue():设置初始值,默认为null
  • public void set(T value):设置一个要保存的值,并会覆盖原来的值
  • public T get():获得保存的值,如果没有用过set方法,会获取到初始值
  • public void remove():移除保存的值
一般 ThreadLocal 作为全局变量使用,示例如下:
public class ConnectionManager {

    // 线程内共享Connection,ThreadLocal通常是全局的,支持泛型
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();

    public static Connection getCurrentConnection() {
        Connection conn = threadLocal.get();
        try {
            if(conn == null || conn.isClosed()) {
                String url = "jdbc:mysql://localhost:3306/test" ;
                conn = DriverManager.getConnection(url , "root" , "root") ;
                threadLocal.set(conn);
            }
        } catch (SQLException e) {
        }
        return conn;
    }

    public static void close() {
        Connection conn = threadLocal.get();
        try {
            if(conn != null && !conn.isClosed()) {
                conn.close();
                threadLocal.remove();
                conn = null;
            }
        } catch (SQLException e) {
        }
    }
}

ThreadLocal解决共享参数

ThreadLocal 也常用在多线程环境中,某个方法处理一个业务,需要递归依赖其他方法时,而要在这些方法中共享参数的问题。

例如有方法 a(),在该方法中调用了方法b(),而在方法 b() 中又调用了方法 c(),即 a–>b—>c。如果 a,b,c 现在都需要使用一个字符串参数 args。

常用的做法是 a(String args)–>b(String args)—c(String args)。但是使用ThreadLocal,可以用另外一种方式解决:

  • 在某个接口中定义一个ThreadLocal 对象
  • 方法 a()、b()、c() 所在的类实现该接口
  • 在方法 a()中,使用 threadLocal.set(String args) 把 args 参数放入 ThreadLocal 中
  • 方法 b()、c() 可以在不用传参数的前提下,在方法体中使用 threadLocal.get() 方法就可以得到 args 参数

示例如下:

interface ThreadArgs {
    ThreadLocal threadLocal = new ThreadLocal();
}

class A implements ThreadArgs {
    public static void a(String args) {
        threadLocal.set(args);
    }
}

class B implements ThreadArgs {
    public static void b() {
        System.out.println(threadLocal.get());
    }
}

class C implements ThreadArgs {
    public static void c() {
        System.out.println(threadLocal.get());
    }
}

public class ThreadLocalDemo {
    public static void main(String[] args) {
        A.a(“hello”);
        B.b();
        C.c();
    }
}

输出结果:

hello
hello

关于 InheritableThreadLocal

InheritableThreadLocal 类是 ThreadLocal 类的子类。ThreadLocal 中每个线程拥有它自己的值,与 ThreadLocal 不同的是,InheritableThreadLocal 允许一个线程以及该线程创建的所有子线程都可以访问它保存的值。

猜你喜欢

转载自blog.csdn.net/weixin_43320847/article/details/83045529
今日推荐