父子线程间变量传递问题

 

 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();
    }
}

 

 
posted @ 2022-06-10 00:41  Peter.Jones  阅读(80)  评论(0)    收藏  举报