下午没事的时候,写了一个关于ThreadLocal的例子。
以前对这个一直没有太在意,看书的时候基本上没有用过。以前写代码的时候,外加做web开发的,基本上用到Thread的东西很少。当时以为这个ThreadLocal以就Thread里面的一个东西。 就没太在意。下午没事,把这个想起来了,写了一个简单的例子。费话少说:
package
com.websidesoft.java;
public class User ... {
private String username;
private String password;
private int age;
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
@Override
public String toString() ...{
return getClass().getName()
+ ": username = " + username
+ ", password = " + password
+ ", age = " + age;
}
}
public class User ... {
private String username;
private String password;
private int age;
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
@Override
public String toString() ...{
return getClass().getName()
+ ": username = " + username
+ ", password = " + password
+ ", age = " + age;
}
}
上面是用到一个普通的Bean。下面是实现了ThreadLocal的一个类。不去解释里面的方面的含义了。不明白自己google一下,网上多的是。或者看看API.
package
com.websidesoft.java;
public class ThreadLocalSample extends ThreadLocal ... {
private static ThreadLocalSample local = new ThreadLocalSample();
private User user;
protected Object initialValue() ...{
user = new User();
user.setUsername("Gavin");
user.setPassword("密码");
user.setAge(25);
return user;
}
public User getUser() ...{
return (User)local.get();
}
}
public class ThreadLocalSample extends ThreadLocal ... {
private static ThreadLocalSample local = new ThreadLocalSample();
private User user;
protected Object initialValue() ...{
user = new User();
user.setUsername("Gavin");
user.setPassword("密码");
user.setAge(25);
return user;
}
public User getUser() ...{
return (User)local.get();
}
}
上面的类就实现在了一个简单的ThreadLocal。ThreadLocal对每一个线程保正会取到同一个对象。比如在我们写数据库连接的时候,可以多个DAO都会调用同一个Connection,怎么保存呢?这个正好是最佳使用时机。
下面写了一个简单的测试类,测试我们的结果是否正确。
package
com.websidesoft.java;
import java.util.ArrayList;
import java.util.List;
public class TestThreadLocal extends Thread ... {
public static List<User> list = new ArrayList();
public void run() ...{
for (int i = 0; i < 2; i++) ...{
ThreadLocalSample tt = new ThreadLocalSample();
list.add(tt.getUser());
}
}
public static void main(String[] args) ...{
for (int i = 0; i < 3; i++) ...{
TestThreadLocal ta = new TestThreadLocal();
ta.start();
}
try ...{
sleep(3000);
} catch (InterruptedException e) ...{
e.printStackTrace();
}
Object o1 = TestThreadLocal.list.get(0);
Object o2 = TestThreadLocal.list.get(1);
Object o3 = TestThreadLocal.list.get(2);
Object o4 = TestThreadLocal.list.get(3);
Object o5 = TestThreadLocal.list.get(4);
Object o6 = TestThreadLocal.list.get(5);
System.out.println(o1.equals(o2)); //true
System.out.println(o3.equals(o4)); //true
System.out.println(o1.equals(o4)); //false
System.out.println(o5.equals(o6)); //true
System.out.println(o1.equals(o5)); //false
System.out.println(o2.equals(o6)); //false
}
}
import java.util.ArrayList;
import java.util.List;
public class TestThreadLocal extends Thread ... {
public static List<User> list = new ArrayList();
public void run() ...{
for (int i = 0; i < 2; i++) ...{
ThreadLocalSample tt = new ThreadLocalSample();
list.add(tt.getUser());
}
}
public static void main(String[] args) ...{
for (int i = 0; i < 3; i++) ...{
TestThreadLocal ta = new TestThreadLocal();
ta.start();
}
try ...{
sleep(3000);
} catch (InterruptedException e) ...{
e.printStackTrace();
}
Object o1 = TestThreadLocal.list.get(0);
Object o2 = TestThreadLocal.list.get(1);
Object o3 = TestThreadLocal.list.get(2);
Object o4 = TestThreadLocal.list.get(3);
Object o5 = TestThreadLocal.list.get(4);
Object o6 = TestThreadLocal.list.get(5);
System.out.println(o1.equals(o2)); //true
System.out.println(o3.equals(o4)); //true
System.out.println(o1.equals(o4)); //false
System.out.println(o5.equals(o6)); //true
System.out.println(o1.equals(o5)); //false
System.out.println(o2.equals(o6)); //false
}
}