为什么要使用ThreadLocal:在Java的多线程编程中,为保证多个线程对共享变量的安全访问,通常会使用synchronized来保证同一时刻只有一个线程对共享变量进行操作。这种情况下可以将类变量放到ThreadLocal类型的对象中,使变量在每个线程中都有独立拷贝,不会出现一个线程读取变量时而被另一个线程修改的现象。
简单的来说,ThreadLocal对于不同线程中的变量不共享,每个线程都有一个自己的。
public class ConnThreadLocal {
public static ThreadLocal<String> th = new ThreadLocal<String>();
public void setTh(String value){
th.set(value);
}
public void getTh(){
System.out.println(Thread.currentThread().getName() + ":" + this.th.get());
}
public static void main(String[] args) throws InterruptedException {
final ConnThreadLocal ct = new ConnThreadLocal();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
ct.setTh("张三");
ct.getTh();
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
ct.setTh("李四");
ct.getTh();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t2");
t1.start();
t2.start();
}
}
输出的两个的到的值是不一样的,说明他们都有自己的ThreadLocal,他们并没有共享一个。
然后附上一个一个使用ThreadLocal的实例,数据库连接池(待修饰版,简版)
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javassist.expr.NewArray;
import com.mysql.jdbc.Connection;
import scala.annotation.meta.getter;
import scala.math.Numeric.IntIsIntegral;
public class MyConnectionPool {
private int minConn = 10; //初始化连接池的最小连接数
private int maxConn = 1000; //连接池的最大连接数量
private String url;
private String userName;
private String password;
private static MyConnectionPool myConnectionPool;
private HashMap<String,Connection> connMap = new HashMap<String,Connection>();
private List<String> connList = new ArrayList<String>();
private List<String> busyList = new ArrayList<String>();
/**
* 私有化构造方法,进行单例创建连接池
*/
private MyConnectionPool(int minConn, int maxConn) throws Exception {
this.minConn = minConn;
this.maxConn = maxConn;
init();
}
/**
* 初始化连接池
*/
private void init() throws Exception {
for (int i = 0; i < minConn; i++) {
createConnection();
}
}
/**
* 的到连接池对象的方法
*/
public static MyConnectionPool getInstance(int minConn, int maxConn) throws Exception {
if (myConnectionPool == null) {
myConnectionPool = new MyConnectionPool(minConn, maxConn);
}
return myConnectionPool;
}
/**
* 获得连接池
* @return 返回一个连接池的实例
*/
public Connection getConnetion() throws ClassNotFoundException,
SQLException, InterruptedException {
if (connList.size() == 0 && busyList.size() >= maxConn) {
while (true) {
Thread.sleep(1000);
if (connList.size() > 0) {
break;
}
}
}
if (connList.size() == 0 && busyList.size() < maxConn) {
createConnection();
}
busyList.add(connList.get(0));
Connection conn = connMap.get(connList.get(0));
ThreadLocal<Connection> tl = new ThreadLocal<>();
ThreadLocal<String> t2 = new ThreadLocal<>();
tl.set(conn);
t2.set(connList.get(0));
connList.remove(0);
return tl.get();
}
/**
* 连接池回收
*/
public void collectConnect() {
ThreadLocal<String> tl = new ThreadLocal<>();
String id=tl.get();
busyList.remove(id);
connList.add(id);
}
/**
* 创建单个连接
*/
public void createConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(url,
userName, password);
String id=System.currentTimeMillis()+"";
connList.add(id);
connMap.put(id, conn);
}
}