什么环境下使用ThreadLocal,(不要说hibernate,还没学)

thinkinjava123 2009-11-01 02:17:28
什么环境下使用ThreadLocal,(不要说hibernate,还没学)

最好有例子和使用环境 一般是什么情况下需要解决什么问题才用?
...全文
176 14 打赏 收藏 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
捏造的信仰 2009-11-02
  • 打赏
  • 举报
回复
看这里:
http://blog.csdn.net/yidinghe/archive/2009/08/09/4427548.aspx
haojia0716 2009-11-02
  • 打赏
  • 举报
回复
比如struts2的Action 被创建时和一个ThreadLocal的副本绑定 修改这个副本不会影响到其他Action 这就保证了Action的线程安全
blliy117 2009-11-01
  • 打赏
  • 举报
回复
楼主真有趣,确实在hibernate中用到这个类超多。

最通俗的意思就是将一个对象绑定一个线程,只管自己用。当然还是可以使用共享对象,只是原则上是最好自己用的对象才放到这个ThreadLocal 中。

ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线程中的对象是该线程自己使用的对象,其他线程是不需要访问的,也访问不到的。各个线程中访问的是不同的对象。

一下子还真解释不完,这东西是难点
可以参考:http://www.itpub.net/thread-987225-1-1.html
swandragon 2009-11-01
  • 打赏
  • 举报
回复
ThreadLocal为解决多线程程序的并发问题
  • 打赏
  • 举报
回复
在 J2SE 环境中 ThreadLocal 最有用的应用是在一种被称为“事务上下文”的 J2EE 设计模式中,也就是使用 ThreadLocal 绑定当前线程所使用 Connection,在调用时可以跨越多个类和方法,实现无侵入式的 JDBC 事务处理。
sforiz 2009-11-01
  • 打赏
  • 举报
回复
即实现线程同步,线程安全
loveofmylife 2009-11-01
  • 打赏
  • 举报
回复
为不同的线程维持一个变量副本,结局变量共享的竞争问题,看需求了,当然典型就是hibernate的sessionFactory
一头头 2009-11-01
  • 打赏
  • 举报
回复
For multithreaded applications where objects with local data may be called by more than one thread, a third type of data is often required. This is data that is shared within the same thread but that is different across threads. This is achieved by a special type of object called a thread-local object. If a thread-local object is declared as static, then the object holds a different value for each thread that uses the object. Thread-local objects are created from the ThreadLocal class.

Now the class:


public class withStaticData {

...

public static ThreadLocal threadSharedClass;

public ThreadLocal threadSharedObject;

}


will have one copy of threadSharedClass per thread that uses the class, whereas threadSharedObject will have one copy per thread per instance of the class withStaticData.

Consider, for example, a secure server that requires a client to log in before allowing it to call its methods. The login method returns a password that must be presented by the thread each time it issues a method call. Now the server could save a mapping between threads and passwords. However, this is tedious and error prone. Thread-local data provides a simple and elegant solution. First, a class is provided, which allocates a new password:


public class Password {

public Password();

// Generates a new password.

public String getPassword();

// Returns the password.

public boolean match(String pass)

// Returns true if pass is the password.

}


Now the server can use thread-local data to hold the password. The calls to the set and get methods are directed to the data associated with the calling thread.


  public class SecureService {

private ThreadLocal password = new ThreadLocal();

public String login() {

Password pass = new Password();

password.set(pass);

return pass.getPassword();

}

public void service(String pass) throws Exception {

Password check = (Password) password.get();

if(check.match(pass)) {

// perform service

} else throw new Exception("no access allowed");

}

}
gzbtiantian 2009-11-01
  • 打赏
  • 举报
回复
并发时的 有些类不是线程安全的,这里就需要用theadlocal来保证
haojia0716 2009-11-01
  • 打赏
  • 举报
回复
ThreadLocal也可以当全局变量使用
临远 2009-11-01
  • 打赏
  • 举报
回复
多线程时需要用。

Spring Security中控制权限,用的也是这个。
冰思雨 2009-11-01
  • 打赏
  • 举报
回复
多线程编码时常会用到这个类。

你想拥有一个线程自己访问的数据区域么?
一般情况下,都是通过,对线程类设置成员变量来实现的。
但如果线程类成员变量的代码不让你更改呢?
在你线程所要调用(执行)的代码中,使用ThreadLocal类吧,它可以帮您解决这个问题。

当然,上述只是我的个人观点,数据区域也可以看成是一个类(或多个类)对象。
xiazhili 2009-11-01
  • 打赏
  • 举报
回复
关注
Yanbin_Q 2009-11-01
  • 打赏
  • 举报
回复
线程本地,就是变量绑定到某个线程时才用。这样可以保证线程安全,以及只要在线程范围内随时都可以方便取用。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧