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"); } }
可以参考:http://www.itpub.net/thread-987225-1-1.html
一般情况下,都是通过,对线程类设置成员变量来实现的。
但如果线程类成员变量的代码不让你更改呢?
在你线程所要调用(执行)的代码中,使用ThreadLocal类吧,它可以帮您解决这个问题。当然,上述只是我的个人观点,数据区域也可以看成是一个类(或多个类)对象。
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"); } }
http://blog.csdn.net/yidinghe/archive/2009/08/09/4427548.aspx