其他分享
首页 > 其他分享> > 父子线程间变量传递问题

父子线程间变量传递问题

作者:互联网

 

 https://blog.csdn.net/a837199685/article/details/52712547

 

package com.cn.xiaonuo.main.test.module;

import com.cn.xiaonuo.core.tenant.entity.TenantInfo;

public class TenantTest {

    public static void main(String[] args) {
        //变量
        TenantInfo tenantInfo = new TenantInfo();
        tenantInfo.setCode("shibei");

        //变量可父子线程传递
        InheritableThreadLocal<TenantInfo> t1 = new InheritableThreadLocal<>();
        t1.set(tenantInfo);

        //变量无法父子线程传递
        ThreadLocal<TenantInfo> t2 = new ThreadLocal<>();
        t2.set(tenantInfo);

        new Thread(() -> {
            System.out.println(t1.get());
            System.out.println(t2.get());
        }).start();
    }
}

 

 

标签:变量,tenantInfo,t2,t1,父子,线程,new,TenantInfo
来源: https://www.cnblogs.com/smileblogs/p/16361730.html