一,、概述 //創(chuàng)建線程局部變量session,用來保存Hibernate的Session
public static final ThreadLocal session = new ThreadLocal(); /** * 獲取當(dāng)前線程中的Session * @return Session * @throws HibernateException */ public static Session currentSession() throws HibernateException { Session s = (Session) session.get(); // 如果Session還沒有打開,,則新開一個Session if (s == null) { s = sessionFactory.openSession(); session.set(s); //將新開的Session保存到線程局部變量中 } return s; } public static void closeSession() throws HibernateException { //獲取線程局部變量,,并強制轉(zhuǎn)換為Session類型 Session s = (Session) session.get(); session.set(null); if (s != null) s.close(); } } 在這個類中,由于沒有重寫ThreadLocal的initialValue()方法,,則首次創(chuàng)建線程局部變量session其初始值為null,第一次調(diào)用currentSession()的時候,,線程局部變量的get()方法也為null,。因此,對session做了判斷,,如果為null,,則新開一個Session,并保存到線程局部變量session中,,這一步非常的關(guān)鍵,,這也是“public static final ThreadLocal session = new ThreadLocal()”所創(chuàng)建對象session能強制轉(zhuǎn)換為Hibernate Session對象的原因。 2,、另外一個實例 創(chuàng)建一個Bean,,通過不同的線程對象設(shè)置Bean屬性,保證各個線程Bean對象的獨立性,。 /** * Created by IntelliJ IDEA. * User: leizhimin * Date: 2007-11-23 * Time: 10:45:02 * 學(xué)生 */ public class Student { private int age = 0; //年齡 public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } } /** * Created by IntelliJ IDEA. * User: leizhimin * Date: 2007-11-23 * Time: 10:53:33 * 多線程下測試程序 */ public class ThreadLocalDemo implements Runnable { //創(chuàng)建線程局部變量studentLocal,,在后面你會發(fā)現(xiàn)用來保存Student對象 private final static ThreadLocal studentLocal = new ThreadLocal(); public static void main(String[] agrs) { ThreadLocalDemo td = new ThreadLocalDemo(); Thread t1 = new Thread(td, "a"); Thread t2 = new Thread(td, "b"); t1.start(); t2.start(); } public void run() { accessStudent(); } /** * 示例業(yè)務(wù)方法,用來測試 */ public void accessStudent() { //獲取當(dāng)前線程的名字 String currentThreadName = Thread.currentThread().getName(); System.out.println(currentThreadName + " is running!"); //產(chǎn)生一個隨機數(shù)并打印 Random random = new Random(); int age = random.nextInt(100); System.out.println("thread " + currentThreadName + " set age to:" + age); //獲取一個Student對象,,并將隨機數(shù)年齡插入到對象屬性中 Student student = getStudent(); student.setAge(age); System.out.println("thread " + currentThreadName + " first read age is:" + student.getAge()); try { Thread.sleep(500); } catch (InterruptedException ex) { ex.printStackTrace(); } System.out.println("thread " + currentThreadName + " second read age is:" + student.getAge()); } protected Student getStudent() { //獲取本地線程變量并強制轉(zhuǎn)換為Student類型 Student student = (Student) studentLocal.get(); //線程首次執(zhí)行此方法的時候,,studentLocal.get()肯定為null if (student == null) { //創(chuàng)建一個Student對象,并保存到本地線程變量studentLocal中 student = new Student(); studentLocal.set(student); } return student; } } 運行結(jié)果: a is running! thread a set age to:76 b is running! thread b set age to:27 thread a first read age is:76 thread b first read age is:27 thread a second read age is:76 thread b second read age is:27 可以看到a,、b兩個線程age在不同時刻打印的值是完全相同的,。這個程序通過妙用ThreadLocal,既實現(xiàn)多線程并發(fā),,游兼顧數(shù)據(jù)的安全性,。 四、總結(jié) ThreadLocal使用場合主要解決多線程中數(shù)據(jù)數(shù)據(jù)因并發(fā)產(chǎn)生不一致問題,。ThreadLocal為每個線程的中并發(fā)訪問的數(shù)據(jù)提供一個副本,,通過訪問副本來運行業(yè)務(wù),,這樣的結(jié)果是耗費了內(nèi)存,單大大減少了線程同步所帶來性能消耗,,也減少了線程并發(fā)控制的復(fù)雜度,。 ThreadLocal不能使用原子類型,只能使用Object類型,。ThreadLocal的使用比synchronized要簡單得多,。 ThreadLocal和Synchonized都用于解決多線程并發(fā)訪問。但是ThreadLocal與synchronized有本質(zhì)的區(qū)別,。synchronized是利用鎖的機制,,使變量或代碼塊在某一時該只能被一個線程訪問。而ThreadLocal為每一個線程都提供了變量的副本,,使得每個線程在某一時間訪問到的并不是同一個對象,,這樣就隔離了多個線程對數(shù)據(jù)的數(shù)據(jù)共享。而Synchronized卻正好相反,,它用于在多個線程間通信時能夠獲得數(shù)據(jù)共享,。 Synchronized用于線程間的數(shù)據(jù)共享,而ThreadLocal則用于線程間的數(shù)據(jù)隔離,。 當(dāng)然ThreadLocal并不能替代synchronized,它們處理不同的問題域,。Synchronized用于實現(xiàn)同步機制,比ThreadLocal更加復(fù)雜,。 五,、ThreadLocal使用的一般步驟 1、在多線程的類(如ThreadDemo類)中,,創(chuàng)建一個ThreadLocal對象threadXxx,,用來保存線程間需要隔離處理的對象xxx。 2,、在ThreadDemo類中,,創(chuàng)建一個獲取要隔離訪問的數(shù)據(jù)的方法getXxx(),在方法中判斷,,若ThreadLocal對象為null時候,,應(yīng)該new()一個隔離訪問類型的對象,并強制轉(zhuǎn)換為要應(yīng)用的類型,。 3,、在ThreadDemo類的run()方法中,通過getXxx()方法獲取要操作的數(shù)據(jù),,這樣可以保證每個線程對應(yīng)一個數(shù)據(jù)對象,,在任何時刻都操作的是這個對象。 通常在多線程中,當(dāng)使用ThreadLocal維護變量時,,ThreadLocal為每個使用該變量的線程提供獨立的變量副本,,所以每一個線程都可以獨立地改變自己的副本,而不會影響其它線程所對應(yīng)的副本 實現(xiàn)線程本地類其實不難:以當(dāng)前線程為key,要保存的對象為value public class ThreadLocalSample { private Map map = Collections.synchronizedMap(new HashMap());
public void set(Object value) { map.put(Thread.currentThread(), value); } public Object get() { Object value = map.get(Thread.currentThread()); return value; } } ThreadLocal可用在Servlet的過濾器中,,比如:每一個請求都由一個Connection來操作數(shù)據(jù)庫,,由于Servlet只有一個實例,所以應(yīng)該是每個線程用一個連接,,而不是所有線程用一個Connection,,因此需要用到ThreadLocal類;由于Servlet是在Filter鏈的調(diào)用中點執(zhí)行,,即Filter1->Filter2->...->Servlet->Filter2->Filter1,,因此:可以用一個Filter,在這個Filter調(diào)用下一個Filter前初始化連接,,并將其放入ThreadLocal中,,在調(diào)用又回到這個Filter時,得到連接并關(guān)閉,。 代碼如下: public class TransactionManageFilter implements Filter { private FilterConfig config; public void init(FilterConfig config) throws ServletException { } public void destroy() { Connection conn = ConnectionManager.currentConnection(); try { conn.setAutoCommit(false); chain.doFilter(request, response, chain); conn.commit(); } catch (Exception e) { conn.rollback(); } finally { try { conn.setAutoCommit(true); conn.close(); ConnectionManager.removeConnection(); } catch (Exception e) {} } } } public class ConnectionManager { private static ThreadLocal currConn = new ThreadLocal();
public static Connection currentConnection() { Object obj = currConn.get(); if (obj != null) { return (Connection)obj; } else { Connection conn = ConnectionFactory.getConnection(); currConn.set(conn); return conn; } } public static void removeConnection() { Object obj = currConn.get(); if (obj != null) { currConn.set(null); } } } |
|