ThreadLocal类及应用技巧

本文详细介绍了ThreadLocal类及其在多线程编程中的应用,包括线程范围内共享数据、多线程同步操作等核心概念。通过实例展示了如何在不同线程间共享变量,并使用ThreadLocal来实现线程隔离的高效数据管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* ThreadLocalShareData.java
* cn.com.songjy.test.socket.thread
* Function: TODO
*
* version date author
* ──────────────────────────────────
* 1.0 2013-8-16 songjy
*
* Copyright (c) 2013, TNT All Rights Reserved.
*/

package cn.com.songjy.test.socket.thread;

import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* ClassName:ThreadLocalShareData
* ThreadLocal类及应用技巧
* @author songjy
* @version 1.0
* @since v1.0
* @Date 2013-8-16 下午4:14:56
*/

public class ThreadLocalShareData {

private static Log log = LogFactory.getLog(ThreadLocalShareData.class);
private static ThreadLocal<Integer> thread_data = new ThreadLocal<Integer>();
//多个变量要求在线程范围内共享可以封装到一个对象中进行存储
private static ThreadLocal<MyThreadScopeData> my_thread_data = new ThreadLocal<MyThreadScopeData>();
private static int data;

public static void main(String[] args) {

for(int i=0; i<5; i++)
new Thread(new Runnable() {

@Override
public void run() {
data = new Random().nextInt();
int data = new Random().nextInt();
log.info(Thread.currentThread().getName() + " has put data :" + data);
//thread_data.put(Thread.currentThread(), data);
thread_data.set(data);
//my_thread_data.set(new MyThreadScopeData(""+data, 12));
MyThreadScopeData.getInstance().setName("name-"+data);
MyThreadScopeData.getInstance().setAge(data);
new A().get();
new B().get();
}
}).start();
}

static class A {
public void get(){
//int data = thread_data.get(Thread.currentThread());
int data = thread_data.get();
//MyThreadScopeData my = my_thread_data.get();
MyThreadScopeData my = MyThreadScopeData.getInstance();
log.info("A from " + Thread.currentThread().getName() + " get data :" + data);
log.info("A from " + Thread.currentThread().getName() + " get MyThreadScopeData :" + my.getName());
}
}

static class B {
public void get(){
//int data = thread_data.get(Thread.currentThread());
int data = thread_data.get();
//MyThreadScopeData my = my_thread_data.get();
MyThreadScopeData my = MyThreadScopeData.getInstance();
log.info("B from " + Thread.currentThread().getName() + " get data :" + data);
log.info("B from " + Thread.currentThread().getName() + " get MyThreadScopeData :" + my.getName());
}
}
}

//该类被编写成了线程范围内共享的数据类
class MyThreadScopeData {

private MyThreadScopeData(){}

/*单例之饱汉模式
private static MyThreadScopeData instance = new MyThreadScopeData();
public static synchronized MyThreadScopeData getInstance(){
return instance;
}*/

/*单例之饿汉模式
private static MyThreadScopeData instance = null;

public static synchronized MyThreadScopeData getInstance(){

if(null == instance)
instance = new MyThreadScopeData();

return instance;
}*/

/*线程范围内共享模式*/
public static MyThreadScopeData getInstance(){

MyThreadScopeData instance = map.get();

if(null == instance) {
instance = new MyThreadScopeData();
map.set(instance);
}

//return map.get();//也可
return instance;
}

private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<MyThreadScopeData>();

private String name;
private int age;

/*MyThreadScopeData(String name, int age){
this.name = name;
this.age = 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;
}

}


引自
[url]http://down.51cto.com/data/443416[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值