import java.util.HashMap;import java.util.Map;import java.util.Random;

/** * Created by chengtao on 17/12/3. *  继5 之后,ThreadLocal就是系统为我们提供的那个map */public class Thread0601_ThreadLocal {

    private static int data = 0;    private static  ThreadLocal<Integer> threadLocalData = new ThreadLocal<Integer>();    public static void main(String[] args) {        for(int i=0;i<2;i++){            new Thread(new Runnable(){                public void run() {                    int data = new Random().nextInt();                    System.out.println(Thread.currentThread().getName()                            + " has put data :" + data);                    threadLocalData.set(data);                    new Thread0601_ThreadLocal.A().get();                    new Thread0601_ThreadLocal.B().get();                }            }).start();        }    }

    static class A{        public void get(){            int data = threadLocalData.get();            System.out.println("A from " + Thread.currentThread().getName()                    + " get data :" + data);        }    }

    static class B{        public void get(){            int data = threadLocalData.get();            System.out.println("B from " + Thread.currentThread().getName()                    + " get data :" + data);        }    }}

------------------------------------------------------------------------------------------------------------------------
import java.util.HashMap;import java.util.Map;import java.util.Random;/** * Created by chengtao on 17/12/3. * 继6.1 之后,如果共享多个变量?如何隐藏 ThreadLocal 类? */public class Thread0602_ThreadLocal {

    private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();    private static ThreadLocal<MyThreadScopeData> myThreadScopeData = new ThreadLocal<MyThreadScopeData>();    public static void main(String[] args) {        for(int i=0;i<2;i++){            new Thread(new Runnable(){                public void run() {                    int data = new Random().nextInt();                    System.out.println(Thread.currentThread().getName()                            + " has put data :" + data);                    x.set(data);/*             MyThreadScopeData myData = new MyThreadScopeData();               myData.setName("name" + data);               myData.setAge(data);               myThreadScopeData.set(myData);*/                    MyThreadScopeData.getThreadInstance().setName("name" + data);                    MyThreadScopeData.getThreadInstance().setAge(data);                    new A().get();                    new B().get();                }            }).start();        }    }

    static class A{        public void get(){            int data = x.get();            System.out.println("A from " + Thread.currentThread().getName()                    + " get data :" + data);/*       MyThreadScopeData myData = myThreadScopeData.get();;         System.out.println("A from " + Thread.currentThread().getName()               + " getMyData: " + myData.getName() + "," +               myData.getAge());*/            MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();            System.out.println("A from " + Thread.currentThread().getName()                    + " getMyData: " + myData.getName() + "," +                    myData.getAge());        }    }

    static class B{        public void get(){            int data = x.get();            System.out.println("B from " + Thread.currentThread().getName()                    + " get data :" + data);            MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();            System.out.println("B from " + Thread.currentThread().getName()                    + " getMyData: " + myData.getName() + "," +                    myData.getAge());        }    }}

class MyThreadScopeData{    private MyThreadScopeData(){}    public static /*synchronized*/ MyThreadScopeData getThreadInstance(){        MyThreadScopeData instance = map.get();        if(instance == null){            instance = new MyThreadScopeData();            map.set(instance);        }        return instance;    }    //private static MyThreadScopeData instance = null;//new MyThreadScopeData();    private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<MyThreadScopeData>();

    private String name;    private int age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

转载于:https://www.cnblogs.com/ctaixw/p/7967798.html

java 多线程 day06 threadLocal相关推荐

  1. java多线程:ThreadLocal详解

    场景: 登录用户的信息保存与获取问题. 在常规的系统设计中,后端系统通常会有一个很长的调用链路(Controller->Service->Dao). 通常用户在登陆之后,用户信息会保存在s ...

  2. Java 多线程:InheritableThreadLocal 实现原理

    前言 介绍 InheritableThreadLocal 之前,假设对 ThreadLocal 已经有了一定的理解,比如基本概念,原理,如果没有,可以参考:Java 多线程:threadlocal关键 ...

  3. java 多线程 一个博客

    http://blog.csdn.net/a352193394/article/category/2563875 Java多线程之~~~线程安全容器的非阻塞容器 在并发编程中,会经常遇到使用容器.但是 ...

  4. bat 等大厂常问的Java多线程面试题,3万字解析

    1 基本概括 2 文章详情 1.1 Java基础--Java多线程(进程与线程的介绍) 1.2 Java基础--Java多线程(线程的创建方式) 1.3 Java基础--Java多线程(什么是进程?) ...

  5. Java多线程中的ThreadLocal,可继承,可修改

    Java多线程中的ThreadLocal,可继承,可修改. package test;import java.util.Date;public class InheritableThreadLocal ...

  6. java多线程之线程本地数据ThreadLocal

    layout: post title: "java多线程之线程本地数据ThreadLocal" subtitle: " "每个线程都有自己的数据,互不干扰.&q ...

  7. Java多线程 | 详解ThreadLocal实现原理

    一.ThreadLocal的简介: 一般情况下,我们创建的变量都是可以给任何线程访问并修改的,如果我们想让线程拥有自己的私有本地变量,那我们就可以使用ThreadLocal类是实现这样的想法. Thr ...

  8. 40个Java多线程问题总结

    (转) 这篇文章作者写的真是不错 40个问题汇总 1.多线程有什么用? 一个可能在很多人看来很扯淡的一个问题:我会用多线程就好了,还管它有什么用?在我看来,这个回答更扯淡.所谓"知其然知其所 ...

  9. Java多线程常见面试题及答案汇总1000道(春招+秋招+社招)

    Java多线程面试题以及答案整理[最新版]Java多线程高级面试题大全(2021版),发现网上很多Java多线程面试题都没有答案,所以花了很长时间搜集,本套Java多线程面试题大全,汇总了大量经典的J ...

最新文章

  1. python怎么导入文件-Python 导入文件过程图解
  2. SAP Classification(物料特性)
  3. python实现JWT
  4. 多记,多问为什么,最古老的学习方法怎么能丢?!
  5. 涨姿势了!delete后加 limit是个好习惯么?
  6. tensorflow pb ckpt pbtxt
  7. fft算法的c语言实现,快速傅立叶变换(FFT)算法(蝶形算法)的C/C++源代码(zz)
  8. 测试人员的工作及介绍
  9. Window10 Excel复制粘贴卡死
  10. linux解密shadow_Linux /etc/shadow(影子文件)内容解析(超详细)
  11. 这是浙江大学郑强教授的经典语录 虽然我不完全赞同但对他的精神佩服的五体投地...
  12. HTTP/2 协议规范
  13. java罗马帝国下载,Java程序设计2020满分完整版考 试题库大全
  14. Redis常见面试题整理
  15. Word文件打开的时候需要输入密码?
  16. ABAP-01 SAP基础
  17. 【笔记】python中的for循环(遍历列表)、for循环中的一些缩进问题
  18. 【学习求职必备】认真认识一下世界末日那年成立的“华为诺亚方舟实验室”...
  19. 基于梁氏—克里曼信息流的因果分析理论及应用——应用部分
  20. win7计算机怎么优化驱动器,Win7优化电脑加快关机速度的方法技巧

热门文章

  1. java unsafe 详解_Java CAS操作与Unsafe类详解
  2. python十大必备知识_学Python必备的基础知识
  3. php 5.6 zend opcache,使用Zend OpCache 提高 PHP 5.5+ 性能
  4. Elasticsearch IK分词器
  5. TensorFlow models/research
  6. python __call__
  7. vCenter Events
  8. App后台开发运维和架构实践学习总结(6)——App客户端与后台交互方式总结
  9. Myeclipse学习总结(2)——MyEclipse快捷键大全
  10. 10用户账户控制只有否_【新书连载】测试工程师核心开发技术(10)—数据库体系结构...