-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58773a3
commit 098c671
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/java/cn/edu/learn/interview/thread/communication/ConnectionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cn.edu.learn.interview.thread.communication; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* @description: 使用threadLocal实现数据库的链接 | ||
* @author: hey | ||
* @date 2016/3/28 | ||
* @version: 1.0 | ||
*/ | ||
public class ConnectionManager { | ||
private static Log log = LogFactory.getLog(ConnectionManager.class); | ||
private static ThreadLocal<Connection> connections = new ThreadLocal<Connection>() { | ||
@Override | ||
protected Connection initialValue() { | ||
Connection con = null; | ||
|
||
try { | ||
|
||
Class.forName("com.mysql.jdbc.Driver"); | ||
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} catch (ClassNotFoundException e) { | ||
log.debug("找不到驱动类"); | ||
} | ||
return con; | ||
} | ||
}; | ||
|
||
public static Connection getConnection() { | ||
return connections.get(); | ||
} | ||
|
||
public void setConnection(Connection con) { | ||
connections.set(con); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/cn/edu/learn/interview/thread/communication/threadLocal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
ThreadLocal������ | ||
1��������˵��ÿ���̶߳�ά���ű����ĸ����������ұ������ֿ���ȥ����ThreadLocal�� | ||
|
||
�����ҿ���������뻹�Dz��ܹ����⣬��Ϊ��һֱȥѰ��ʹ��clone()�ĵط�������һֱû���ҵ� | ||
|
||
�����ҷ��������ϵ��뷨���Լ�����������Դ�룬�������ÿ���߳��ﱣ��Ķ�����clone�����ģ� | ||
|
||
��ʵ����new�����Ķ��� | ||
|
||
2��Դ������� | ||
ThreadLocal��get()������ | ||
|
||
public T get() { | ||
Thread t = Thread.currentThread(); | ||
ThreadLocalMap map = getMap(t); | ||
if (map != null) { | ||
ThreadLocalMap.Entry e = map.getEntry(this); | ||
if (e != null) | ||
return (T)e.value; | ||
} | ||
return setInitialValue(); | ||
} | ||
���Դ����Ҫ�漰������������ | ||
|
||
��һ�����õ���ǰ�̣߳����߳��л��һ��ThreadLocalMap�Ķ�����������������������ν�����ĵط� | ||
|
||
�ڶ����������ֿ��ܣ� | ||
1�����ThreadLocalMap����Ϊ���Ҹö����е�Ԫ�ز�Ϊ�գ��ʹӸö����л�ȡ���������ҷ��� | ||
|
||
2������Ϊ�գ�����setInitialValue����������ʼ�� | ||
|
||
ThreadLocal.setInitialValue����Դ�� | ||
|
||
private T setInitialValue() { | ||
T value = initialValue(); | ||
Thread t = Thread.currentThread(); | ||
ThreadLocalMap map = getMap(t); | ||
if (map != null) | ||
map.set(this, value); | ||
else | ||
createMap(t, value); | ||
return value; | ||
} | ||
���Դ����Ҫ�漰���������� | ||
|
||
��һ��������initialValue()���������û�ó�ʼֵ���÷�������null�����ǿ�����д�÷�������������ConnectionManager���е����ӡ� | ||
|
||
�ڶ�������ֵ���õ�map�У������ظ�ֵ |