ThreadLocal线程范围内的共享变量

简介: 数据库connection和strut2每个请求用到ThreadLocalimport java.util.Random;public class ThreadLocalTest {    private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();    /*     * pr



数据库connection和strut2每个请求用到ThreadLocal

import java.util.Random;

public class ThreadLocalTest {
    private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();

    /*
     * private static ThreadLocal<MyThreadScopeData> myThreadScoprData = new
     * ThreadLocal<MyThreadScopeData>();
     */

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {

            new Thread(new Runnable() {

                @Override
                public void run() {
                    int data = new Random().nextInt();
                    x.set(data);
                    System.out.println(Thread.currentThread().getName()
                            + " has put data:" + data);
                    /*
                     * MyThreadScopeData myData = new MyThreadScopeData();
                     * myData.setName("yuanhai" + data); myData.setAge("29");
                     * myThreadScoprData.set(myData);
                     */
                    MyThreadScopeData.getThreadInstance().setName(
                            "yuanhai" + data);
                    MyThreadScopeData.getThreadInstance().setAge("29");
                    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.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 String name;
    private String age;
    private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<MyThreadScopeData>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

}


本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1716581

目录
相关文章
|
1月前
|
存储 Java 测试技术
ThreadLocal:线程专属的变量
ThreadLocal:线程专属的变量
41 0
|
3月前
|
存储 Java 数据安全/隐私保护
【JUC】ThreadLocal 如何实现数据的线程隔离?
【1月更文挑战第15天】【JUC】ThreadLocal 如何实现数据的线程隔离?ThreadLocal 导致内存泄漏问题?
|
27天前
|
安全 Java
java中线程经常被问到ThreadLocal你懂吗?
java中线程经常被问到ThreadLocal你懂吗?
7 0
|
29天前
|
算法 安全 Unix
【C++ 20 信号量 】C++ 线程同步新特性 C++ 20 std::counting_semaphore 信号量的用法 控制对共享资源的并发访问
【C++ 20 信号量 】C++ 线程同步新特性 C++ 20 std::counting_semaphore 信号量的用法 控制对共享资源的并发访问
29 0
|
1月前
|
Java 关系型数据库 MySQL
【数据库连接,线程,ThreadLocal三者之间的关系】
【数据库连接,线程,ThreadLocal三者之间的关系】
21 0
|
1月前
|
存储 安全 Java
调用链跨线程传递 ThreadLocal 对象对比
说起本地线程专属变量,大家首先会想到的是 JDK 默认提供的 ThreadLocal,用来存储在整个链路中都需要访问的数据,并且是线程安全的。由于在落地全链路压测的过程中,一个基本并核心的功能需求是流量标记需要在整个链路中进行传递,那么线程上下文环境成为解决这个问题最合适的技术。
42 2
调用链跨线程传递 ThreadLocal 对象对比
|
1月前
|
存储 安全 Java
多线程------ThreadLocal详解
多线程------ThreadLocal详解
|
2月前
|
存储 安全 Python
什么是Python中的线程局部存储(Thread Local Storage)?
【2月更文挑战第3天】【2月更文挑战第6篇】
|
2月前
|
安全 Python
如何在Python中处理多线程之间的共享状态?
如何在Python中处理多线程之间的共享状态?
|
14天前
|
存储 Java 数据库连接
java多线程之线程通信
java多线程之线程通信

热门文章

最新文章