网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java多线程-ThreadLocal-创新互联

ThreadLocal:每个线程自身的存储本地、局部区域,类似于容器,每个线程都会在其中有一定存储空间
常用的方法get/set/initialValue
官方建议为private static
每个线程存储自己的数据,更改不会影响其他线程
ThreadLocal 子类InheritableThreadLocal:
继承上下文环境的数据

创新互联建站,专注为中小企业提供官网建设、营销型网站制作、响应式网站开发、展示型成都做网站、网站设计等服务,帮助中小企业通过网站体现价值、有效益。帮助企业快速建站、解决网站建设与网站营销推广问题。
public class my {

//Integer 初始值为null
//private static ThreadLocal threadlocal=new ThreadLocal<>();
//更改初始值需要创建ThreadLocal的子类重写initialValue或者使用lambda(jdk8)
private static ThreadLocal threadlocal=new ThreadLocal ()
        {//父类匿名内部类重写方法
            protected Integer initialValue()
            {
                return 200;
            }
        };
//private static ThreadLocal threadlocal=new ThreadLocal.withInitial(()->200);

public static void main(String[]args) throws InterruptedException
{
    //获取值
    System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
    //设置值
    threadlocal.set(99);
    System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());

    new Thread(new test()).start();
    new Thread(new test()).start();
}

public static class test implements Runnable
{
    public void run()
    {
        threadlocal.set((int)(Math.random()*100));
        System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
    }
}

}

每个线程自身的数据更改不会影响其他线程

public class my {

private static ThreadLocal threadlocal=new ThreadLocal ()
        {//父类匿名内部类重写方法
            protected Integer initialValue()
            {
                return 1;
            }
        };

public static void main(String[]args) throws InterruptedException
{
    for(int i=0;i<5;i++)
    {
        new Thread(new test()).start();
    }
}

public static class test implements Runnable
{
    public void run()
    {
        //修改不会影响其他线程
        Integer left=threadlocal.get();
        System.out.println(Thread.currentThread().getName()+"-->"+left);
        threadlocal.set(left-1);
        System.out.println(Thread.currentThread().getName()+"还剩下"+threadlocal.get());
    }
}

}

分析ThreadLocal运行环境:

public class my {

private static ThreadLocal threadlocal=new ThreadLocal ()
        {//父类匿名内部类重写方法
            protected Integer initialValue()
            {
                return 1;
            }
        };

public static void main(String[]args) throws InterruptedException
{
    new Thread(new test()).start();//两个打印的内容在不同的线程里
    //当start()之后,线程才从main转换到其他线程,所以构造器里的是main线程的
}

public static class test implements Runnable
{
    public test()
    {
        threadlocal.set(100);  //属于main线程,修改不会影响start内的线程
        System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
    }
    public void run()
    {
        System.out.println(Thread.currentThread().getName()+"还剩下"+threadlocal.get());
    }
}

}

子类InheritableThreadLocal:

public class my {

private static ThreadLocal threadlocal=new InheritableThreadLocal<>();

public static void main(String[]args) throws InterruptedException
{
    System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
    threadlocal.set(2);

    //此线程由main线程开辟,拷贝一份main线程的数据给此线程
    //即将main线程里的2给此线程
    new Thread(()->{
        System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
        threadlocal.set(200);
        System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
    }).start();
}

}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


本文标题:java多线程-ThreadLocal-创新互联
URL网址:http://bjjierui.cn/article/ehhep.html

其他资讯