什么环境下使用ThreadLocal,(不要说hibernate,还没学)最好有例子和使用环境  一般是什么情况下需要解决什么问题才用?

解决方案 »

  1.   

    多线程时需要用。Spring Security中控制权限,用的也是这个。
      

  2.   

    ThreadLocal也可以当全局变量使用
      

  3.   

    为不同的线程维持一个变量副本,结局变量共享的竞争问题,看需求了,当然典型就是hibernate的sessionFactory
      

  4.   

    在 J2SE 环境中 ThreadLocal 最有用的应用是在一种被称为“事务上下文”的 J2EE 设计模式中,也就是使用 ThreadLocal 绑定当前线程所使用 Connection,在调用时可以跨越多个类和方法,实现无侵入式的 JDBC 事务处理。 
      

  5.   

    ThreadLocal为解决多线程程序的并发问题
      

  6.   

    楼主真有趣,确实在hibernate中用到这个类超多。最通俗的意思就是将一个对象绑定一个线程,只管自己用。当然还是可以使用共享对象,只是原则上是最好自己用的对象才放到这个ThreadLocal 中。ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线程中的对象是该线程自己使用的对象,其他线程是不需要访问的,也访问不到的。各个线程中访问的是不同的对象。 一下子还真解释不完,这东西是难点
    可以参考:http://www.itpub.net/thread-987225-1-1.html
      

  7.   

    多线程编码时常会用到这个类。你想拥有一个线程自己访问的数据区域么?
    一般情况下,都是通过,对线程类设置成员变量来实现的。
    但如果线程类成员变量的代码不让你更改呢?
    在你线程所要调用(执行)的代码中,使用ThreadLocal类吧,它可以帮您解决这个问题。当然,上述只是我的个人观点,数据区域也可以看成是一个类(或多个类)对象。
      

  8.   

    并发时的 有些类不是线程安全的,这里就需要用theadlocal来保证 
      

  9.   

    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");    }  }
      

  10.   

    比如struts2的Action 被创建时和一个ThreadLocal的副本绑定 修改这个副本不会影响到其他Action 这就保证了Action的线程安全
      

  11.   

    看这里:
    http://blog.csdn.net/yidinghe/archive/2009/08/09/4427548.aspx