ThreadLocal的使用
1前言
在多线程中,有时会使用到类ThreadLocal,为了弄清楚其中的意义,特地翻看了源代码,总结了一下,但是其中有自己的想法,不免有错误,见谅。
2概述
该类并不是Thread,而是提供了线程局部变量。功能比较简单。就是为每一个使用该变量的线程都提供一个变量值的副本,即每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突。这里有个预备知识,对于jvm来说,分为主工作区和线程工作区。意义比较简单,主工作区是实例的所有线程共有,线程工作区是实例的每个线程专有的工作区,其中包括一些主工作区的一些实例字段数据的拷贝。这其中还有很多知识,必须先学习一下关于多线程的有关知识。如果不明白就很难理解这个类的意义和使用,但这些不在本文档的范围之内。
3ThreadLocal的设计
它的位置在包java.lang下面。里面有一个静态内部类ThreadLocalMap,类似于hashMap的设计,用于存储每一个线程的变量的副本,就不多说了。还包括四个方法。
1 initialValue():该方法是一个受保护的方法,而且它在类中的返回值是null。基于这两点已经很明显它是应该由子类去实现的,通常使用一个内部匿名类对ThreadLocal进行子类化,该方法返回此线程局部变量的当前线程的初始值,这个方法只有在一个线程第1次调用get()或者set(Object)时才执行,并且仅执行1次。
2 get(): 返回此线程局部变量的当前线程副本中的值。
3 set(Object value): 将此线程局部变量的当前线程副本中的值设置为指定值.
4 remove():移除此线程局部变量的值。在jdk1.5中使用。
在API中给出的例子也是比较好的,如下:
public class SerialNum {
// The next serial number to be assigned
private static int nextSerialNum = 0;
private static ThreadLocal serialNum = new ThreadLocal() {
protected synchronized Object initialValue() {
return new Integer(nextSerialNum++);
}
};
public static int get() {
return ((Integer) (serialNum.get())).intValue();
}
}
每个线程都保持一个对其线程局部变量副本的隐式引用,只要线程是活动的并且 ThreadLocal 实例是可访问的;在线程消失之后,其线程局部实例的所有副本都会被垃圾回收(除非存在对这些副本的其他引用)。
4区别
ThreadLocal和其它所有的同步机制都是为了解决多线程中的对同一变量的访问冲突,在普通的同步机制中,是通过对象加锁来实现多个线程对同一变量的安全访问的。在1.5以前的版本中,synchronized是自动释放锁。在JDK1.5的版本中,提供了类java.util.concurrent.locks.Lock。它比synchronized更精确和有更高的性能。这时该变量是多个线程共享的,使用这种同步机制需要有较强的多线程基础和编程经验,因为需要知道对变量进行读写的时机,什么时候需要锁定这个对象,又什么时候需要释放该对象的锁等等很多问题。所有这些都是因为多个线程共享了资源造成的。而ThreadLocal就从另一个角度来解决多线程的并发访问,ThreadLocal会为每一个线程维护一个和该线程绑定的变量的副本,从而隔离了多个线程的数据,每一个线程都拥有自己的变量副本,从而也就没有必要对该变量进行同步了。ThreadLocal提供了线程安全的共享对象,在编写多线程代码的时候,就可以把不安全的变量封装进ThreadLocal,当然也可以把该对象的特定于线程的状态封装进ThreadLocal。
5总结
同步机制是为了同步多个线程对相同资源的并发访问,是为了多个线程之间进行通信的有效方式;而ThreadLocal是隔离多个线程的数据共享。
个人认为,如果有较强的多线程的基础,还是应该使用synchronized来进行控制。ThreadLocal的出现只是为了简化一部分人的编程,使编程更容易。减小出错的概率。
当然,既然JDK提供了这样的功能,在适当的情况可以使用。所以,如果你需要进行多个线程之间进行通信,则使用同步机制;如果需要隔离多个线程之间的共享冲突,可以使用ThreadLocal。
Threadlocal模式,和singleton不一样,singleton是说在整个应用程序中保证只有一个实例,而Threadlocal是指每线程有唯一实例。Servlet的 “单实例,多线程”是指多个线程共享一个实例,因此会有同步的问题
Threadlocal 测试:
AnotherThread.java
package cn.com.xinli.usp.test;
public class AnotherThread implements Runnable
{
public void run(){
TestThreadLocal ttl = new TestThreadLocal();
int number1 = ttl.get();
System.out.println("another number1 is " + number1);
}
}
TestThreadLocal.java
package cn.com.xinli.usp.test;
public class TestThreadLocal
{
// The next serial number to be assigned
private static int nextSerialNum = 0;
private static ThreadLocal serialNum = new ThreadLocal()
{
protected synchronized Object initialValue()
{
return new Integer(nextSerialNum++);
}
};
public static int get() {
return ((Integer) (serialNum.get())).intValue();
}
public static void main(String[] args)
{
System.out.println("@@@");
System.out.println("%%%");
TestThreadLocal ttl = new TestThreadLocal();
int number1 = ttl.get();
System.out.println("number1 is " + number1);
int number2 = ttl.get();
System.out.println("number2 is " + number2);
new Thread(new AnotherThread()).start();
new Thread(new AnotherThread()).start();
}
}
运行结果:
number1 is 0
number2 is 0
another number1 is 1
another number1 is 2
分析:
由此可见,对于同一个线程,都维护一个自己的局部变量。这在并发环境中可以很好的防止线程间共享资源的冲突。ThreadLocal 实例通常是类中的私有静态字段,它们希望将状态与某一个线程(例如,用户 ID 或事务 ID)相关联。
使用ThreadLocal 模式写的 1。数据库帮助类 2。HibernateUtil 帮助类
1.数据库帮助类 (ConnectionFactory.java ,TransactionUtil.java)
package cn.com.xinli.usp.test;
import java.sql.*;
import cn.com.xinli.usp.db.common.DBUtil;
/**
*
* <p>Title:数据库链接工厂 </p>
* @author 罗代均 ldj_work@126.com
* @version 1.0
*/
public class ConnectionFactory
{
private static ConnectionFactory instance = new ConnectionFactory();
private ConnectionFactory()
{
}
public static ConnectionFactory getInstace() {
return instance;
}
public static Connection getConnection()
{
Connection conn = (Connection) TransactionUtil.local.get();
System.out.println("%%^^:"+TransactionUtil.local.get());
try {
if (conn == null || conn.isClosed())
{
conn = ConnectionFactory.buildConnection();
TransactionUtil.local.set(conn);
conn.setAutoCommit(false);
return conn;
}
else
{
return conn;
}
} catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
}
//具体获得数据库连接方法,这里为了演示,使用jdbc直接获得,实际项目可从DataSource获得连接
public static Connection buildConnection()
{
System.out.println("conn***********");
try
{
return DBUtil.getConnection();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
/*
String url =
"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "123";
try
{
Class.forName(driver).newInstance();
System.out.println("Build Connection......");
return DriverManager.getConnection(url, user, password);
}
catch (Exception ex1)
{
ex1.printStackTrace();
return null;
}
*/
}
public static void close(ResultSet rs, Statement st,Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
}
rs = null;
}
if (st != null)
{
try {
st.close();
} catch (SQLException ex1) {
}
st = null;
}
if(conn!=null)
{
try {
conn.close();
} catch (SQLException ex1) {
}
conn = null;
}
}
}
TransactionUtil.java
package cn.com.xinli.usp.test;
import java.sql.Connection;
import java.sql.SQLException;
public class TransactionUtil
{
private static final ConnectionFactory connFactory;
static
{
connFactory = ConnectionFactory.getInstace();
}
public static final ThreadLocal local = new ThreadLocal();
/**
* 开始事务
*/
public static void beginTransaction() {
Connection conn = (Connection) local.get();
try {
if (conn == null || conn.isClosed()) {
conn = connFactory.buildConnection();
local.set(conn);
}
conn.setAutoCommit(false);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
/**
* 提交事务
*/
public static void commit() {
Connection conn = (Connection) local.get();
if (conn != null) {
try {
conn.commit();
conn.setAutoCommit(true);
} catch (SQLException ex) {
throw new RuntimeException(ex.getMessage());
}
}
}
/**
* 回滚事务
*/
public static void rollback() {
Connection conn = (Connection) local.get();
if (conn != null)
{
try
{
System.out.println("rollback........");
conn.rollback();
} catch (SQLException ex)
{
throw new RuntimeException(ex.getMessage());
}
}
}
/**
* 结束事务
*/
public static void endTransaction() {
Connection conn = (Connection) local.get();
local.set(null);
if (conn != null) {
try {
System.out.println("close connection.....");
conn.close();
} catch (SQLException ex) {
throw new RuntimeException(ex.getMessage());
}
conn = null;
}
}
}
使用方法:
try
{
PreparedStatement psmt = null;
ResultSet rs=null;
Connection conn=ConnectionFactory.getConnection();
psmt=conn.prepareStatement("select * from usp_log");
rs=psmt.executeQuery();
while(rs.next())
{
System.out.println(rs.getString(1));
}
ConnectionFactory.close(rs,psmt,conn);
}
catch(Exception e)
{
e.printStackTrace();
}
事务处理:
try {
TransactionUtil.benginTransaction(); //开始事务
dao.add(emp);
//其它业务逻辑
TransactionUtil.commit(); //提交事务
TransactionUtil.endTransaction //结束事务
} catch (DaoException ex) {
this.roolback(); //出现异常,会滚事务
throw new ServiceException(ex.getMessage());
}
HibernateUtil.java帮助类
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final Logger log = Logger.getLogger(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
log.info("HibernateUtil:开始初始化Hibernate SessionFactory对象 *****************************************************************************");
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("创建SessionFactory失败:",ex);
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
log.info("*************************************************** HibernateUtil:结束初始化Hibernate SessionFactory对象 ");
}
public static final ThreadLocal tLocalsess = new ThreadLocal();
public static final ThreadLocal tLocaltx = new ThreadLocal();
// 取得Session
public static Session currentSession() throws Exception {
Session session = (Session) tLocalsess.get();
// 打开一个新的session,如果当前不可用
try {
if (session == null || !session.isOpen()) {
session = openSession();
tLocalsess.set(session);
}
} catch (HibernateException e) {
log.error("获得Session失败",e);
e.printStackTrace();
throw new Exception("获得session失败",e);
}
return session;
}
// 关闭Session
public static void closeSession() {
Session session = (Session) tLocalsess.get();
tLocalsess.set(null);
try {
if (session != null && session.isOpen())
{
session.close();
log.debug("关闭session");
}
} catch (HibernateException e) {
log.error("关闭Session失败",e);
e.printStackTrace();
}
}
// 开始事务
public static void beginTransaction() throws Exception {
// 声明Transaction类型对象tx,并赋初值
Transaction tx = (Transaction) tLocaltx.get();
try {
if (tx == null) {
tx = currentSession().beginTransaction();
tLocaltx.set(tx);
}
} catch (HibernateException e) {
log.error("开始事务失败",e);
e.printStackTrace();
throw new Exception("开始事务失败",e);
}
}
// 关闭事务
public static void comitTransaction() throws Exception {
Transaction tx = (Transaction) tLocaltx.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.commit();
tLocaltx.set(null);
}
} catch (HibernateException e) {
log.error("提交事务失败:",e);
e.printStackTrace();
throw new Exception("提交事物失败",e);
}
}
// 事务回滚
public static void rollbackTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
tLocaltx.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
} catch (HibernateException e) {
log.error("事务回滚失败:",e);
e.printStackTrace();
}
}
private static Session openSession() throws HibernateException {
return getSessionFactory().openSession();
}
public static SessionFactory getSessionFactory() throws HibernateException {
return sessionFactory;
}
}