将ThreadLocal类传递给另一个线程?

时间:2017-11-08 02:23:23

标签: java multithreading

我在Java程序中使用ThreadLocal,以便通过线程获取和设置某些数据成员。

我有一个包含以下内容的课程

public class ThreadLocalManager {
    public static final ThreadLocal<String> accountUsername = new ThreadLocal<String>();
    public static final ThreadLocal<String> accountPassword = new ThreadLocal<String>();
    public static final ThreadLocal<Long> accountId = new ThreadLocal<Long>();
    .....................
    .....................
    .....................

}

我在多线程应用程序中使用它没有任何问题。现在每个线程都将创建另一个线程,我希望将ThreadLocalManager传递给创建的新线程。怎么做?

1 个答案:

答案 0 :(得分:2)

编辑:我修改了原始建议(下面)中的示例代码以使用InheritableThreadLocal,我发现它更加简单,因此添加了更新后的代码。

这是更新的ThreadLocalManager:

package inheritableThreadLocal;

public class ThreadLocalManager {
    public static final InheritableThreadLocal<String> accountUsername = new InheritableThreadLocal<String>();
    public static final InheritableThreadLocal<String> accountPassword = new InheritableThreadLocal<String>();
    public static final InheritableThreadLocal<Long> accountId = new InheritableThreadLocal<Long>();
}

我还更新了Runner(参见下面的原文),但现在它唯一的目的是打印变量的值:

package inheritableThreadLocal;

public class Runner implements Runnable{

    @Override
    public void run() {
        System.out.println("Inside Runner's run");
        System.out.println(ThreadLocalManager.accountId.get());
        System.out.println(ThreadLocalManager.accountUsername.get());
        System.out.println(ThreadLocalManager.accountPassword.get());
    }

}

最后更新的类包含main方法:

package inheritableThreadLocal;

public class ThreadLocalMain {

    public static void main(String[] args) {
        System.out.println("At start of main");
        System.out.println(ThreadLocalManager.accountId.get());
        System.out.println(ThreadLocalManager.accountUsername.get());
        System.out.println(ThreadLocalManager.accountPassword.get());

        Thread t1 = new Thread(new Runnable(){

            @Override
            public void run() {
                ThreadLocalManager.accountId.set(new Long(12345));
                ThreadLocalManager.accountUsername.set("user1");
                ThreadLocalManager.accountPassword.set("pass1");

                System.out.println("In t1 run");
                System.out.println(ThreadLocalManager.accountId.get());
                System.out.println(ThreadLocalManager.accountUsername.get());
                System.out.println(ThreadLocalManager.accountPassword.get());

                Thread t2 = new Thread(new Runner());
                t2.start();
            }

        });
        t1.start();
    }

}

原始建议:

因此,您可以使用Thread个对象创建新的Runnable个对象。如果这样做,您可以创建实现Runnable的自定义对象,并可以保留ThreadLocal变量的值,直到新的Thread开始。新的Thread启动后(即调用run方法),这些自定义Runnable对象会设置ThreadLocal个变量。

这是一个我为此创建的主要课程:

package threadLocal;

public class ThreadLocalMain {

    public static void main(String[] args) {
        System.out.println("At start of main");
        System.out.println(ThreadLocalManager.accountId.get());
        System.out.println(ThreadLocalManager.accountUsername.get());
        System.out.println(ThreadLocalManager.accountPassword.get());

        Thread t1 = new Thread(new Runnable(){

            @Override
            public void run() {
                ThreadLocalManager.accountId.set(new Long(12345));
                ThreadLocalManager.accountUsername.set("user1");
                ThreadLocalManager.accountPassword.set("pass1");

                System.out.println("In t1 run");
                System.out.println(ThreadLocalManager.accountId.get());
                System.out.println(ThreadLocalManager.accountUsername.get());
                System.out.println(ThreadLocalManager.accountPassword.get());

                Thread t2 = new Thread(new Runner(ThreadLocalManager.accountUsername.get(),
                        ThreadLocalManager.accountPassword.get(),
                        ThreadLocalManager.accountId.get()));
                t2.start();
            }

        });
        t1.start();
    }

}

为了完整性,这里是我使用的ThreadLocalManager:

package threadLocal;

public class ThreadLocalManager {
    public static final ThreadLocal<String> accountUsername = new ThreadLocal<String>();
    public static final ThreadLocal<String> accountPassword = new ThreadLocal<String>();
    public static final ThreadLocal<Long> accountId = new ThreadLocal<Long>();
}

最后,这是我创建的自定义Runnable

package threadLocal;

public class Runner implements Runnable{

    private String userName, password;
    private long acctId;

    public Runner(String user, String pword, long accountId){
        System.out.println("In Runner constructor");
        System.out.println(ThreadLocalManager.accountId.get());
        System.out.println(ThreadLocalManager.accountUsername.get());
        System.out.println(ThreadLocalManager.accountPassword.get());

        userName = user;
        password = pword;
        acctId = accountId;
    }

    @Override
    public void run() {
        System.out.println("Inside Runner's run");
        System.out.println(ThreadLocalManager.accountId.get());
        System.out.println(ThreadLocalManager.accountUsername.get());
        System.out.println(ThreadLocalManager.accountPassword.get());

        ThreadLocalManager.accountId.set(acctId);
        ThreadLocalManager.accountUsername.set(userName);
        ThreadLocalManager.accountPassword.set(password);

        System.out.println("Inside Runner's run - after setting");
        System.out.println(ThreadLocalManager.accountId.get());
        System.out.println(ThreadLocalManager.accountUsername.get());
        System.out.println(ThreadLocalManager.accountPassword.get());
    }

}

此代码将ThreadLocal变量的值传递到Runner实例,然后使用Runner实例创建新的Thread(正如您将看到的那样)如果您运行代码,则首先没有ThreadLocal变量的值。然后Runner设置ThreadLocal个变量,正如您从控制台输出中看到的那样,它们与t1的匹配。

希望这有帮助!