java 多线程 day06 threadLocal

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;    }}
时间: 2024-10-17 11:42:48

java 多线程 day06 threadLocal的相关文章

java多线程模式ThreadLocal原理简述及其使用详解

原创整理不易,转载请注明出处:java多线程模式ThreadLocal原理简述及其使用详解 代码下载地址:http://www.zuidaima.com/share/1781557457128448.htm ThreadLocal是为了使每个线程保存一份属于自己的数据. 先看一个使用ThreadLocal的实例. package com.zuidaima.aop.framework; import com.zuidaima.core.NamedThreadLocal; public abstra

Java 多线程之--ThreadLocal 简介

<span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> 在多线程开发中,经常会遇见在run方法里面调用一个公共的属性的事情,由于每次start都会创建一个线程,因此</span> <span style="font-size: 18px; font-family: Arial,

java多线程学习-ThreadLocal

为了凑字,把oracle文档里介绍ThreadLocal抄过来 public class ThreadLocal<T> extends Object This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its ow

java 多线程(threadlocal)

package com.example; import java.util.Random; public class App { public static class MyRunnable1 implements Runnable { //ThreadLocal是一个线程局部变量 private ThreadLocal<String> threadlocal = new ThreadLocal<String>(); @Override public void run() { //

java多线程--------深入分析 ThreadLocal 内存泄漏问题

前言 ThreadLocal 的作用是提供线程内的局部变量,这种变量在线程的生命周期内起作用,减少同一个线程内多个函数或者组件之间一些公共变量的传递的复杂度.但是如果滥用ThreadLocal,就可能会导致内存泄漏.下面,我们将围绕三个方面来分析ThreadLocal 内存泄漏的问题 ThreadLocal 实现原理 ThreadLocal为什么会内存泄漏 ThreadLocal 最佳实践 ThreadLocal 实现原理 ThreadLocal的实现是这样的:每个Thread 维护一个 Thr

Java多线程与并发应用-(5)-如何优雅的使用ThreadLocal类

内容来自,张孝祥老师的张孝祥-Java多线程与并发库高级应用>视频教程 package com.lipeng; public class MyThreadLocalTest2 { private final static MyThreadLocal<String> myThreadLocal=new MyThreadLocal<String>(); /** * @param args */ public static void main(String[] args) { ne

java多线程18: ThreadLocal的作用

从上一篇对于ThreadLocal的分析来看,可以得出结论:ThreadLocal不是用来解决共享对象的多线程访问问题的,通过ThreadLocal的set()方法设置到线程的ThreadLocal.ThreadLocalMap里的是是线程自己要存储的对象,其他线程不需要去访问,也是访问不到的.各个线程中的ThreadLocal.ThreadLocalMap以及ThreadLocal.ThreadLocal中的值都是不同的对象. 至于为什么要使用ThreadLocal,不妨这么考虑这个问题.Ja

跟我学Java多线程——ThreadLocal

ThreadLocal是什么 ThreadLocal这个词如果直接翻译就是"本地线程",可是如果真的按"本地线程"来理解,那就确实大错特错了,ThreadLocal它并不是一个Thread,它跟Thread确实有关系,是用来维护Thread的有关变量的,把它命名为ThreadLocalVariable可能更容易让人理解,在多线程中ThreadLocal为变量在每个线程中都创建了一个跟特定线程有关的变量的副本,这样就可以使每个线程在运行中只可以使用与自己线程有关的特定

Rhythmk 一步一步学 JAVA (21) JAVA 多线程

1.JAVA多线程简单示例 1.1 .Thread  集成接口 Runnable 1.2 .线程状态,可以通过  Thread.getState()获取线程状态: New (新创建) Runnable (可以运行) Blocked  (被阻塞) Waiting  (等待) Timed waiting (计时等待) Terminated  (被终止) ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27